设为首页 收藏本站
查看: 1087|回复: 0

[经验分享] 15.Nginx负载均衡&SSL密钥对&Nginx配置SSL

[复制链接]

尚未签到

发表于 2018-11-10 10:54:41 | 显示全部楼层 |阅读模式
  [toc]


  扩展
  针对请求的uri来代理 http://ask.apelearn.com/question/1049
  根据访问的目录来区分后端的web http://ask.apelearn.com/question/920
  nginx长连接  http://www.apelearn.com/bbs/thread-6545-1-1.html
  nginx算法分析   http://blog.sina.com.cn/s/blog_72995dcc01016msi.html

Nginx负载均衡

  负载均衡在服务端开发中算是一个比较重要的特性。因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理前端,因为Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就可以分发给后台服务端(backend servers,也叫做服务池, 后面简称backend)来做复杂的计算、处理和响应,这种模式的好处是相当多的:隐藏业务主机更安全,节约了公网IP地址,并且在业务量增加的时候可以方便地扩容后台服务器。
  负载均衡可以分为硬件负载均衡和软件负载均衡,前者一般是专用的软件和硬件相结合的设备,设备商会提供完整成熟的解决方案,通常也会更加昂贵。软件的复杂均衡以Nginx占据绝大多数.


1.修改虚拟主机配置文件(以baidu.com为例)
  

[root@xavi vhost]# yum install -y bind-utils  
[root@xavi vhost]# dig baidu.com
  

  
;  DiG 9.9.4-RedHat-9.9.4-51.el7_4.2  baidu.com
  
;; global options: +cmd
  
;; Got answer:

  
;; ->>HEADER  

  

  看到有两个IP,可以看到两个IP,有两个IP就可以走负载均衡了

2.编辑配置文件
  

[root@xavi vhost]# pwd  
/usr/local/nginx/conf/vhost
  
[root@xavi vhost]# vim load.conf
  

  
uptream baidu_com
  
{
  ip_hash;
  server 111.13.101.208:80;
  server 220.181.57.216:80;
  
}
  
server
  
{
  listen 80;
  server_name www.baidu.com;
  location /
  {
  proxy_pass      http://baidu_com;
  proxy_set_header Host   $host;
  proxy_set_header X-Real-IP      $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  
}
  

#配置内容  
upstream baidu_com
  
#名字自定义
  
{
  ip_hash;
  
#   目的:同一个用户保持在同一个服务器上
  
#   即当域名指向多个IP时,保证每个用户始终解析到同一IP
  server 111.13.101.208:80;
  server 220.181.57.216:80;
  
#  指定web服务器的IP
  
}
  

  

3.测试
  由于指向dig qq.com时候也是有问题,只能获得一个IP,百度有两个IP打算拒绝访问.
  

[root@xavi vhost]# curl -x127.0.0.1 baidu.com -I  
curl: (7) Failed connect to 127.0.0.1:1080; 拒绝连接
  

二、ssl原理

SSL工作流程
DSC0000.jpg


  浏览器发送一个https的请求给服务器;
  服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
  服务器会把公钥传输给客户端;
  客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
  客户端把加密后的随机字符串传输给服务器;
  服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
  服务器把加密后的数据传输给客户端;
  客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;


三、生成ssl密钥对

1.生成key即“私钥”:openssl genrsa
  

[root@xavi conf]# openssl genrsa -des3 -out tmp.key 2048  
Generating RSA private key, 2048 bit long modulus
  
...+++
  
...................+++
  
e is 65537 (0x10001)
  
Enter pass phrase for tmp.key:
  
Verifying - Enter pass phrase for tmp.key:
  

  //这一步操作是生成key即“私钥”,2048为加密字符长度,会让我们输入密码,不能太短,否者不成功。

2.把上一步生成的tmp.key在转换成xavilinux.key
  

[root@xavi conf]# openssl rsa -in tmp.key -out xavilinux.key  
Enter pass phrase for tmp.key:
  
writing RSA key
  

  把tmp.key转化成zlinux.key,目的是删除刚才设置的密码,如果不清除密码,后续nginx加载它的时候要输入它的密码,很不方便.

3.生成证书请求文件
  生成证书请求文件,key文件和csr文件生成最终的公钥文件。Common Name为后面配置Nginx配置文件server_name
  

[root@xavi conf]# rm -f tmp.key  
[root@xavi conf]# openssl req -new -key xavilinux.key -out xavilinux.csr
  
You are about to be asked to enter information that will be incorporated
  
into your certificate request.
  
What you are about to enter is what is called a Distinguished Name or a DN.
  
There are quite a few fields but you can leave some blank
  
For some fields there will be a default value,
  
If you enter '.', the field will be left blank.
  
-----
  
Country Name (2 letter code) [XX]:86
  
State or Province Name (full name) []:jiangsu
  
Locality Name (eg, city) [Default City]:suzhou
  
Organization Name (eg, company) [Default Company Ltd]:dsfss
  
Organizational Unit Name (eg, section) []:dsf
  
Common Name (eg, your name or your server's hostname) []:xavi.com
  
Email Address []:dsf1626@163.com
  

  
Please enter the following 'extra' attributes
  
to be sent with your certificate request
  
A challenge password []:123456
  
An optional company name []:dsf
  

4.最终生成CRT证书文件
  

[root@xavi conf]# openssl x509 -req -days 365 -in xavilinux.csr -signkey xavilinux.key -out xavilinux.crt  
Signature ok
  
subject=/C=86/ST=jiangsu/L=suzhou/O=dsfss/OU=dsf/CN=xavi.com/emailAddress=dsf1626@163.com
  
Getting Private key
  

  以上操作最终目的是生成xavilinux.key和xavilinux.crt两个文件,其实购买SSL证书主要得到这两个文件,有了这两个文件就可以配置nginx了
  

[root@xavi conf]#  ls |grep xavilinux  
xavilinux.crt
  
xavilinux.csr
  
xavilinux.key
  

四、Nginx配置ssl

1.编辑配置文件
  

[root@xavi ~]# cd /usr/local/nginx/conf/vhost  
[root@xavi vhost]# vim /usr/local/nginx/conf/vhost/ssl.conf
  

  
server
  
{
  listen 443;
  server_name xavi.com;
  index index.html index.php;
  root /data/wwwroot/xavi.com;
  ssl on;
  ssl_certificate xavilinux.crt;
  ssl_certificate_key xavilinux.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  
}
  

2.检查配置文件有无问题,结果说明当前的Nginx不支持SSL
  

[root@xavi vhost]# /usr/local/nginx/sbin/nginx -t  
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
  
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
  

  这说明当前Nginx并不支持SSL,因为之前Nginx编译时并没有配置支持SSL的参数.

3.重新编译一次,加上SSL参数:
  

[root@xavi vhost]# /usr/local/nginx/sbin/nginx -V //了解版本号  
nginx version: nginx/1.12.1
  
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  
configure arguments: --prefix=/usr/local/nginx
  
[root@xavi vhost]#  cd /usr/local/src/nginx-1.12.1
  

  

  确定版本号,找到配置文件中那部分是支持ssl服务的
  

[root@xavi nginx-1.12.1]# ./configure --help |grep -i ssl  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL
  

4.增加支持SSL的参数./configure --prefix=/usr/local/nginx --with-http_ssl_module
  

[root@xavi nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module  
[root@xavi nginx-1.12.1]# make
  
[root@xavi nginx-1.12.1]# make install
  

5. 编译完成,再来检验一次,启动nginx服务
  

[root@xavi nginx-1.12.1]#  /usr/local/nginx/sbin/nginx -t  
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  
[root@xavi nginx-1.12.1]# /etc/init.d/nginx start
  
Starting nginx (via systemctl):                            [  确定  ]
  

  

查看443端口
  

[root@xavi nginx-1.12.1]# netstat -lntp  
Active Internet connections (only servers)
  
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd
  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6156/nginx: master
  
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1779/dnsmasq
  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1047/sshd
  
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1044/cupsd
  
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1589/master
  
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      6156/nginx: master
  
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd
  
tcp6       0      0 :::22                   :::*                    LISTEN      1047/sshd
  
tcp6       0      0 ::1:631                 :::*                    LISTEN      1044/cupsd
  
tcp6       0      0 ::1:25
  

6.没有问题,然后创建对应的目录和测试文件:
  

[root@xavi nginx-1.12.1]# mkdir /data/wwwroot/xavi.com  
mkdir: 无法创建目录"/data/wwwroot/xavi.com": 文件已存在
  
[root@xavi nginx-1.12.1]# cd /data/wwwroot/xavi.com
  
[root@xavi xavi.com]# vi index.html
  

7.curl https://xavi.com报错,编辑/etc/hosts文件,加上xavi.com
DSC0001.jpg

DSC0002.jpg

DSC0003.jpg


再编辑hosts文件,写入一行(hosts路径为C:\Windows\System32\drivers\etc),用浏览器查看
DSC0004.jpg


查看防火墙:iptables -nvL
DSC0005.jpg


关闭防火墙: iptables -F
DSC0006.jpg

  高级网站:12306
DSC0007.jpg




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-633176-1-1.html 上篇帖子: ELK监控日志nginx(集成Geoip) 下篇帖子: Nginx服务优化——性能与安全
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表