Nginx负载均衡、SSL原理、生成SSL密钥对、Nginx配置SSL

6月12日任务

12.17 Nginx负载均衡
12.18 ssl原理
12.19 生成ssl密钥对
12.20 Nginx配置ssl
扩展 
针对请求的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

 

12.17 Nginx负载均衡

通过学习了Nginx代理,那么当只有一台机器的时候可以说是代理,如果有多台机器的时候,那就不能够单靠代理了,需要设置负载均衡。

比如有5个用户访问web服务器,如果其中一个服务器挂了,那么用户1不会再去请求web服务器1,而是自动分配其他的web服务器提供服务,这样就叫负载均衡。

操作步骤:

在正式操作之前可以使用dig命令解析网站域名对应的IP地址,如果没有安装,需要先使用 yum install -y bind-utils 安装dig命令。

设置负载均衡操作步骤:

1、新建ld.conf配置文件

[root@jimmylinux-001 vhost]# vim ld.conf

加入以下内容

upstream qq
{
    ip_hash;
    server 180.163.26.39:80;
    server 58.60.9.21:80;
    server 59.37.96.63:80;
}
server
{
    listen 80;
    server_name www.qq.com;
    location /
    {
        proxy_pass      http://qq;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

2、直接使用curl访问测试

重新-s加载配置文件,然后在curl访问www.qq.com就可以访问到真正的QQ主页了

[root@jimmylinux-001 vhost]# /usr/local/nginx/sbin/nginx -s reload

[root@jimmylinux-001 vhost]# curl -x127.0.0.1:80 www.qq.com

以上操作就是Nginx的负载均衡,另外一个知识点:Nginx代理或负载均衡不支持去代理HTTPS,也就是server后面端口不可以写成443。

 

12.18 ssl原理

 

12.19 生成ssl密钥对

1、进入指定目录并生成私钥文件

[root@jimmylinux-001 ~]# cd /usr/local/nginx/conf
[root@jimmylinux-001 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:              输入密码abcd1234
Verifying - Enter pass phrase for tmp.key:  再次确认密码

2、转换key,取消密码。

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

3、删除tmp.key文件

[root@jimmylinux-001 conf]# rm -f tmp.key

4、生成证书请求文件,目的是为了和私钥一起生成公钥文件。

[root@jimmylinux-001 conf]# openssl req -new -key jimmy.key -out jimmy.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]:China
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:ShenZhen
Locality Name (eg, city) [Default City]:ShenZhen
Organization Name (eg, company) [Default Company Ltd]:jimmy
Organizational Unit Name (eg, section) []:jimmy
Common Name (eg, your name or your server's hostname) []:jimmylinux
Email Address []:joan2008.lms@gmail.com

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

5、用生成的证书请求文件jimmy.csr和之前的jimmy.key私钥文件一起生成公钥文件

[root@jimmylinux-001 conf]# openssl x509 -req -days 365 -in jimmy.csr -signkey jimmy.key -out jimmy.crt

这里的jimmy.key是私钥文件,jimmy.crt是公钥文件。

 

12.20 Nginx配置ssl

已经有公钥和私钥,那么就可以配置Nginx的SSL了。

1、进入指定目录、然后新建一个配置文件。

[root@jimmylinux-001 vhost]# vim ssl.conf    新建配置文件

添加以下内容

server
{
    listen 443;                               监听端口
    server_name jimmy.com;
    index index.html index.php;
    root /data/wwwroot/jimmy.com;
    ssl on;                                   开启SSL支持HTTPS
    ssl_certificate jimmy.crt;                指定公钥
    ssl_certificate_key jimmy.key;            指定私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;      SSL协议
}

2、-t检查配置文件的语法是否正确,如果出现以下报错,因为最早编辑Nginx的时候并没有指定支持SSL,这里需要重新编译nginx,加上--with-http_ssl_module

重新配置完以后,执行make和make install

[root@jimmylinux-001 nginx-1.12.1]# make

[root@jimmylinux-001 nginx-1.12.1]# make install

3、刚才编译完以后就会多一个支持HTTPS的SSL,重新-t检查配置文件语法也没有问题了。

重启Nginx并检查监听端口

[root@jimmylinux-001 nginx-1.12.1]# /etc/init.d/nginx restart  重启Nginx
Restarting nginx (via systemctl):                          [  确定  ]
[root@jimmylinux-001 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:80              0.0.0.0:*               LISTEN      4020/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      934/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1305/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      4020/nginx: master  多一个443的端口
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      1310/php-fpm: maste 
tcp6       0      0 :::22                   :::*                    LISTEN      934/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1305/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1286/mysqld   

4、进入指定目录,然后创建一个测试文件。

[root@jimmylinux-001 nginx-1.12.1]# cd /data/wwwroot/jimmy.com/

[root@jimmylinux-001 jimmy.com]# ls

[root@jimmylinux-001 jimmy.com]# vim 1.txt  新建测试文件

添加以下内容
This is SSL test page.

[root@jimmylinux-001 jimmy.com]# mv 1.txt index.html  重新更名成index.html文件

5、测试访问

[root@jimmylinux-001 jimmy.com]# curl -x127.0.0.1:443 https://jimmy.com/  如果直接使用curl方式访问,就会报400的错误状态码。
curl: (56) Received HTTP code 400 from proxy after CONNECT

需要修改hosts文件,然后再访问才可以。

可以编辑Windows的hosts文件,通过浏览器访问。

如果浏览器打开很慢无法访问,那么就要检查下防火墙了。

[root@jimmylinux-001 jimmy.com]# iptables -nvL  检查如果有防火墙

[root@jimmylinux-001 jimmy.com]# iptables -F    可以直接-F

刷新浏览器就可以访问了,如果不被浏览器认可的证书或者不合法的证书,https都会出现红色的显示。

 

想要有合法认可颁发的证书,可以到这个网站进行购买。 

 

 

 

 

 

 

 

 
posted @ 2018-06-11 23:06  吉米乐享驿站  阅读(1165)  评论(0编辑  收藏  举报