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

1、Nginx负载均衡
php

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


在开始部署负载均衡以前,咱们先来介绍一个命令,dig命令须要yum安装一下nginx

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

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


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

;; global options: +cmdwindows

;; 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端口。


2、SSL原理

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


https的工做流程:

1.png

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

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

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

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

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

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

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

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




3、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

1.png


12306网站是本身颁发的证书:(在中国的政府有些网站,认为只有本身的颁发的安全,因此用本身颁发的证书)

若是想要买证书,能够搜索 沃通,

相关文章
相关标签/搜索