LNMP(Nginx负载均衡,SSL原理,Nginx配置SSL,生产SSL密钥对)

一、Nginx负载均衡

负载均衡:单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。


在开始部署负载均衡之前,我们先来介绍一个命令,dig命令需要yum安装一下

[root@lnmp ~]# yum install bind-utils              

[root@lnmp ~]# dig qq.com            (dig后加域名,他可以返回2个ip.实则域名解析,我们就用这两个ip测试负载均衡)


; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.1 <<>> qq.com

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57513

;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0


;; QUESTION SECTION:

;qq.com. IN A


;; ANSWER SECTION:

qq.com. 601 IN A 125.39.240.113

qq.com. 601 IN A 61.135.157.156


[root@lnmp ~]# vim /usr/local/nginx/conf/vhost/load.conf            (再来编写一个配置文件,需要用到upstream模块,upstream:数据转发功能,为nginx提供了跨越单机的横向处理能力,使nginx摆脱只能为终端节点提供单一功能的限制,而使它具备了网路应用级别的拆分、封装和整合的战略功能。

upstream qq                            

{

    ip_hash;           (负载均衡有多个web服务器,我们需要一个长连接来保持于一个服务器的链接,这里需要用到hash)

    server 61.135.157.156:80;       

    server 125.39.240.113:80;

}

server

{

    listen 80;

    server_name www.qq.com;

    location /

    {

        proxy_pass      http://qq;   (这里写的要与upstream一致,因为域名是虚拟的,下面的2个ip才是重要的)

        proxy_set_header Host   $host;

        proxy_set_header X-Real-IP      $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

}


检查语法错误并且重新加载配置文件。

[root@lnmp ~]# /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@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload


[root@lnmp ~]# curl -x127.0.0.1:80 www.qq.com                (发现返回的是qq页面的源代码)


nginx不支持代理Https服务。也就是说不支持访问web服务器的443端口。


二、SSL原理

https和http相比,https的通信是加密的。如果不加密,比如你访问一个很重要的网站,数据包还是会到达,但是可能会用人从中间复制一份。https会把数据包加密,就算从中间复制也无法解码。


https的工作流程:

LNMP(Nginx负载均衡,SSL原理,Nginx配置SSL,生产SSL密钥对)

  1. 1.浏览器发送一个https的请求给服务器。

  2. 2.服务器有一套数字证书,可以自己制作也可以向组织申请,区别积水自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出 (这套证书其实就是一对公钥和私钥)

  3. 3.服务器会把公钥传输给客户端

  4. 4.客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机字符串,并用收到的公钥加密。

  5. 5.客户端把加密的随机字符串传输给服务器

  6. 6.服务器收到加密随机字符串后,先用私钥解密,获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,也就是将数据和这个随机字符串通过某种算法混合一起,这一除非知道私钥,否则无法获7.取数据内容)

  7. 8.服务器把加密后的数据传输给客户端。

  8. 9.客户端收到数据后,在用自己的私钥也就是那个随机字符串解密。




三、Nginx配置ssl

Nginx配置ssl

[root@lnmp nginx-1.8.0]# vim /usr/local/nginx/conf/vhost/ssl.conf           (编写ssl的配置文件)

server

{

    listen 443;                                        (监听443端口)

    server_name lty.com;                                 (编写server_name)

    index index.html index.php;

    root /data/wwwroot/lty.com;

    ssl on;                                       (开启ssl服务)

    ssl_certificate lty.crt;                           (指定公钥)

    ssl_certificate_key lty.key;                         (指定私钥)

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;                   (指定三种模式)

}

[root@lnmp nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t (如果nginx编译的时候没有加上ssl,这里会报错需要重新编译)


重新编译:

[root@lnmp nginx-1.8.0]# cd /usr/local/src/nginx-1.8.0

[root@lnmp nginx-1.8.0]# ./configure --help |grep -i ssl            

  --with-http_ssl_module             enable ngx_http_ssl_module

  --with-mail_ssl_module             enable ngx_mail_ssl_module

  --with-openssl=DIR                 set path to OpenSSL library sources

  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL

[root@lnmp nginx-1.8.0]# ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module

[root@lnmp nginx-1.8.0]# make && make install


编译完成后就可以检查语法错误了,然后重新启动

[root@lnmp nginx-1.8.0]# /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@lnmp nginx-1.8.0]# /etc/init.d/nginx restart

Restarting nginx (via systemctl):                          [  确定  ]


创建测试页面:

[root@lnmp nginx-1.8.0]# mkdir /data/wwwroot/lty.com

[root@lnmp nginx-1.8.0]# vim /data/wwwroot/lty.com/index.html


因为是我们自己办法的证书,直接修改/etc/hosts,用Curl测试并看不出效果,提示证书已经失去信任。

[root@lnmp nginx-1.8.0]# vim /etc/hosts

127.0.0.1   lty.com

[root@lnmp nginx-1.8.0]# curl https://lty.com

curl: (60) Peer's certificate issuer has been marked as not trusted by the user.

More details here: http://curl.haxx.se/docs/sslcerts.html


curl performs SSL certificate verification by default, using a "bundle"

 of Certificate Authority (CA) public keys (CA certs). If the default

 bundle file isn't adequate, you can specify an alternate file

 using the --cacert option.

If this HTTPS server uses a certificate signed by a CA represented in

 the bundle, the certificate verification probably failed due to a

 problem with the certificate (it might be expired, or the name might

 not match the domain name in the URL).

If you'd like to turn off curl's verification of the certificate, use

 the -k (or --insecure) option.


编辑windows的hosts。

192.168.52.101 lty.com

用浏览器打开

https://lty.com

如果访问不到,查看防火墙信息简单的办法直接-F

LNMP(Nginx负载均衡,SSL原理,Nginx配置SSL,生产SSL密钥对)


12306网站是自己颁发的证书:(在中国的*有些网站,认为只有自己的颁发的安全,所以用自己颁发的证书)

如果想要买证书,可以搜索 沃通,











本文转自 小新锐 51CTO博客,原文链接:http://blog.51cto.com/13407306/2059168,如需转载请自行联系原作者
上一篇:【对讲机的那点事】对讲机开机无声,且过一会儿机壳发热的原因


下一篇:Linux系统下查看目录大小