cdchenli 发表于 2018-11-16 10:34:56

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

  五十、Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl
  一、Nginx负载均衡
  代理一台机器叫代理,代理两台机器就可以叫负载均衡。
  代理服务器后有多个web服务器提供服务的时候,就可以实现负载均衡的功能。
  dig命令:解析域名的IP。常用的域名查询工具,可以用来测试域名系统工作是否正常,可以反馈多个IP。
  需要安装这个包:# yum install -y bind-utils
  # dig qq.com
  ;DiG 9.9.4-RedHat-9.9.4-51.el7_4.2qq.com
  ;; global options: +cmd
  ;; Got answer:

  ;; ->>HEADER  # ping qq.com
  PING qq.com (14.17.32.211) 56(84) bytes of data.
  # cd /usr/local/nginx/conf/vhost/
  # vim load.conf
  upstream qq_com//这个名字可以自定义,这个名字代表着下面的两歌IP。
  {
  ip_hash;       //目的是使同一个用户的访问始终保持在一个机器上
  server 61.135.157.156:80;   当端口是80时此处的:80可以省略
  server 125.39.240.113:80;
  }
  server
  {
  listen 80;                                  定义监听端口
  server_name www.qq.com;    定义域名
  location /
  {
  proxy_pass      http://qq;   做代理时是指定它的IP,此处不能写IP就只能写upstream的名字
  proxy_set_header Host   $host;
  proxy_set_header X-Real-IP      $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  }
  测试:
  # curl -x127.0.0.1:80 www.qq.com正常情况下,会访问默认虚拟主机。
  This is the defaule site.
  # /usr/local/nginx/sbin/nginx -t

  # /usr/local/nginx/sbin/nginx -s>  # curl -x127.0.0.1:80 www.qq.com再次访问时,访问的就是qq的主页。
  反馈回来的是网页的源码。
  知识点:
  Nginx不支持代理https,就是server 125.39.240.113:80冒号后面不能写443,只能代理httpd,新版本能代理tcp。
  如果一定要代理https,访问web服务器的80端口,但不支持443。
  web服务器提供https服务,只能是后端访问的时候是80,代理服务器可以监听443,Nginx访问后端服务器的时候,访问它的80端口,但是不支持访问它的443。
  二、SSL原理
  https的通信是加密的,如果中间被***截取,***获取到的数据包是不能解密的,看到的是乱码。
  HTTPS:一种加密的HTTP协议,HTTP通信的数据包传输过程中被截获,可以破译数据包里面的信息;而使用HTTPS通信,即使被截获,也无法破译里面的内容。
  SSL工作流程:
  1.浏览器发送一个https的请求给服务器;
  2.服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
  3.服务器会把公钥传输给客户端;
  4.客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
  5.客户端把加密后的随机字符串传输给服务器;
  6.服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
  7.服务器把加密后的数据传输给客户端;
  8.客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;

  三、生成SSL密钥对
  # cd /usr/local/nginx/conf
  # rpm -qf `which openssl` 查要这个openssl的包的名字,默认应该安装了
  openssl-1.0.2k-8.el7.x86_64
  # 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文件为私钥。
  //genrsa:生成rsa形式的私钥;2048:长度;名字是tmp.key。
  # openssl rsa -in tmp.key -out mrx.key
  Enter pass phrase for tmp.key:
  writing RSA key
  //转换key,取消密码 。
  //-in:指定要被转换的密钥,-out:指定要输出的密钥。
  //此时tmp.key和mrx.key实际上是一样的,只是tmp.key有密码,mrx.key没密码
  # rm -f tmp.key          //然后删除有密码的。
  # openssl req -new -key mrx.key -out mrx.csr(请求文件)
  //生成证书请求文件,需要拿这个文件和私钥一起生成公钥文件
  Country Name (2 letter code) :11
  State or Province Name (full name) []:changsha
  Locality Name (eg, city) :changsha
  Organization Name (eg, company) :MRX
  Organizational Unit Name (eg, section) []:MRX
  Common Name (eg, your name or your server's hostname) []:MRX111
  Email Address []:mrx@mrxlinux.com
  Please enter the following 'extra' attributes
  to be sent with your certificate request
  A challenge password []:12345
  An optional company name []:MRX
  # openssl x509 -req -days 365 -in mrx.csr -signkey mrx.key -out mrx.crt
  //这里的mrx.crt为公钥
  Signature ok
  subject=/C=11/ST=changsha/L=changsha/O=MRX/OU=MRX/CN=MRX111/emailAddress=mrx@mrxlinux.com
  Getting Private key
  //这样最终才生成了CRT证书文件。
  四、Nginx配置SSL
  # cd /usr/local/nginx/conf/vhost/
  # vim ssl.conf
  server
  {
  listen 443;
  server_name mrx.com;
  index index.html index.php;
  root /data/wwwroot/mrx.com;
  ssl on;          //开启ssl,支持https。
  ssl_certificate mrx.crt;      //指定公钥
  ssl_certificate_key mrx.key;   //指定私钥
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    //协议,一般三种都配置上
  }
  # mkdir /data/wwwroot/mrx.com
  # /usr/local/nginx/sbin/nginx -t   //报错,不知道ssl的配置,因为之前编译Nginx时没有指定支持ssl
  nginx: unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
  nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
  # 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 -V
  nginx version: nginx/1.8.0
  built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
  built with OpenSSL 1.0.2k-fips26 Jan 2017
  TLS SNI support enabled
  configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
  # /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):                        [确定]
  # 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      3952/nginx: master
  tcp      0      0 0.0.0.0:22            0.0.0.0:*               LISTEN      946/sshd
  tcp      0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1301/master
  tcp      0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      3952/nginx: master
  tcp6       0      0 :::22                   :::*                  LISTEN      946/sshd
  tcp6       0      0 ::1:25                  :::*                  LISTEN      1301/master
  tcp6       0      0 :::3306               :::*                  LISTEN      1261/mysqld
  多了一个443。
  # cd /data/wwwroot/mrx.com/
  # vim index.html   //做一个测试文件
  This is ssl.
  # curl -x 127.0.0.1:443 https://mrx.com
  curl: (56) Received HTTP code 400 from proxy after CONNECT
  # vim /etc/hosts
  127.0.0.1mrx.com
  # curlhttps://mrx.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下面还有内容没有贴出来。
  编辑Windows中c盘的hosts文件,加上192.168.93.130 mrx.com
  再浏览器访问https://mrx.com,如果访问不到,查看有无防火墙。
  有的话,简单的方法是直接iptables -F,不想清空就加一条443端口的规则,再次浏览器访问,就会弹出来不安全,还想继续访问,就点高级,继续前往。

  证书不被信任的时候就会报不安全。
  沃通:购买SSL证书的网站。

页: [1]
查看完整版本: 五十、Nginx负载均衡、SSL原理、生成SSL密钥对、Nginx配置SSL-seventeen