shirobert 发表于 2018-11-13 06:19:53

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

  一、Nginx负载均衡
  负载均衡:单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。
  在开始部署负载均衡之前,我们先来介绍一个命令,dig命令需要yum安装一下
  # yum install bind-utils
  # dig qq.com            (dig后加域名,他可以返回2个ip.实则域名解析,我们就用这两个ip测试负载均衡)
  ;DiG 9.9.4-RedHat-9.9.4-51.el7_4.1qq.com
  ;; global options: +cmd
  ;; Got answer:

  ;; ->>HEADER  # curl -x127.0.0.1:80 www.qq.com                (发现返回的是qq页面的源代码)
  nginx不支持代理Https服务。也就是说不支持访问web服务器的443端口。
  二、SSL原理
  https和http相比,https的通信是加密的。如果不加密,比如你访问一个很重要的网站,数据包还是会到达,但是可能会用人从中间复制一份。https会把数据包加密,就算从中间复制也无法解码。
  https的工作流程:


[*]  1.浏览器发送一个https的请求给服务器。
[*]  2.服务器有一套数字证书,可以自己制作也可以向组织申请,区别积水自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出 (这套证书其实就是一对公钥和私钥)
[*]  3.服务器会把公钥传输给客户端
[*]  4.客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机字符串,并用收到的公钥加密。
[*]  5.客户端把加密的随机字符串传输给服务器
[*]  6.服务器收到加密随机字符串后,先用私钥解密,获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,也就是将数据和这个随机字符串通过某种算法混合一起,这一除非知道私钥,否则无法获7.取数据内容)
[*]  8.服务器把加密后的数据传输给客户端。
[*]  9.客户端收到数据后,在用自己的私钥也就是那个随机字符串解密。
  三、Nginx配置ssl
  Nginx配置ssl
  # 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;                   (指定三种模式)
  }
  # /usr/local/nginx/sbin/nginx -t (如果nginx编译的时候没有加上ssl,这里会报错需要重新编译)
  重新编译:
  # cd /usr/local/src/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
  # ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module
  # make && make install
  编译完成后就可以检查语法错误了,然后重新启动
  # /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
  # /etc/init.d/nginx restart
  Restarting nginx (via systemctl):                        [确定]
  创建测试页面:
  # mkdir /data/wwwroot/lty.com
  # vim /data/wwwroot/lty.com/index.html
  因为是我们自己办法的证书,直接修改/etc/hosts,用Curl测试并看不出效果,提示证书已经失去信任。
  # vim /etc/hosts
  127.0.0.1   lty.com
  # 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>  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

  12306网站是自己颁发的证书:(在中国的政府有些网站,认为只有自己的颁发的安全,所以用自己颁发的证书)
  如果想要买证书,可以搜索 沃通,

页: [1]
查看完整版本: LNMP(Nginx负载均衡,SSL原理,Nginx配置SSL,生产SSL密钥对)