码迷,mamicode.com
首页 > 其他好文 > 详细

15.Nginx负载均衡&SSL密钥对&Nginx配置SSL

时间:2018-03-19 10:24:18      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:Nginx负载均衡   SSL密钥对   Nginx配置SSL   

[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<<- opcode: QUERY, status: NOERROR, id: 24796
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;baidu.com.         IN  A

;; ANSWER SECTION:
baidu.com.      174 IN  A   111.13.101.208
baidu.com.      174 IN  A   220.181.57.216

;; Query time: 41 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 日 3月 18 17:28:34 CST 2018
;; MSG SIZE  rcvd: 70

看到有两个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工作流程

技术分享图片

浏览器发送一个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

技术分享图片

技术分享图片

技术分享图片

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

技术分享图片

查看防火墙:iptables -nvL

技术分享图片

关闭防火墙: iptables -F

技术分享图片
高级网站:12306
技术分享图片

15.Nginx负载均衡&SSL密钥对&Nginx配置SSL

标签:Nginx负载均衡   SSL密钥对   Nginx配置SSL   

原文地址:http://blog.51cto.com/12995218/2088297

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!