Nginx负载均衡,ssl原理,生成ssl密钥对,Nginx配置ssl
nginx的负载均衡
- nginx的负载均衡就是把代理服务器指向多个ip,让用户可以通过代理服务器访问到多个web服务器,当其中一个web服务器宕机时,不影响用户访问网站。
- 配置如下
[root@akuilinux01 vhost]# vim load.conf upstream qq_com { ip_hash; server 111.161.64.48:80; server 111.161.64.40:80; } server { listen 80; server_name www.qq.com; location / { proxy_pass http://qq_com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
- upstream来指定多个web server,proxy_pass http://qq_com;这里对应upstream qq_com
- ip_hash保证同一个用户始终保持在同一台服务器
- nginx不支持代理https(端口为443),新版本支持tcp
- dig命令是域名解析工具,安装包bind-utils
ssl原理
- HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议要比http协议安全
- ssl的流程
- 浏览器发送一个https的请求给服务器;
- 服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
- 服务器会把公钥传输给客户端;
- 客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
- 客户端把加密后的随机字符串传输给服务器;
- 服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
- 服务器把加密后的数据传输给客户端;
- 客户端收到数据后,再用自己的私钥也就是那个随机字符串解密
生成ssl密钥对
- yum install -y openssl安装ssl工具
- 放在这个文件下/usr/local/nginx/conf
- openssl genrsa -des3 -out tmp.key 2048 //生成私钥,key文件为私钥
- openssl rsa -in tmp.key -out aminglinux.key //转换key,取消密码
- rm -f tmp.key
- openssl req -new -key aminglinux.key -out aminglinux.csr//生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件
- 该部分如果不购买证书可以自定义;如果是正式应用在网站上,需要规范填写对应信息(需要购买证书)
- openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
//生成公钥,aminglinux.crt为公钥文件Nginx配置SSL
- 配置文件
[root@akuilinux01 conf]# vim /usr/local/nginx/conf/vhost/ssl.conf server { listen 443; server_name aming.com; index index.html index.php; root /data/wwwroot/aming.com; ssl on; #开启ssl ssl_certificate aminglinux.crt; #配置公钥 ssl_certificate_key aminglinux.key; #配置私钥 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #配置协议 } [root@akuilinux01 conf]# mkdir /data/wwwroot/aming.com
- 报错
[root@akuilinux01 conf]# /usr/local/nginx/sbin/nginx -t nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7 nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
- 未识别ssl配置,需要重新编译Nginx
[root@akuilinux01 ~]# cd /usr/local/src/nginx-1.14.0 [root@akuilinux01 ~]# ./configure --prefix=/usr/local/nginx --with-http_ssl_modul [root@akuilinux01 ~]# make [root@akuilinux01 ~]# make install [root@akuilinux01 nginx-1.14.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@akuilinux01 ~]# /etc/init.d/nginx restart Restarting nginx (via systemctl): [ 确定 ] [root@akuilinux01 ~]# 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 5054/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 849/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1221/master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 5054/nginx: master tcp6 0 0 :::22 :::* LISTEN 849/sshd tcp6 0 0 ::1:25 :::* LISTEN 1221/master tcp6 0 0 :::3306 :::* LISTEN 1179/mysqld nginx监听了443端口,表示配置生效了
- 未识别ssl配置,需要重新编译Nginx
- 测试
[root@akuilinux01 ~]# cd /data/wwwroot/aming.com/ [root@akuilinux01 aming.com]# vim index.html this is ssl. [root@akuilinux01 aming.com]# vim /etc/hosts 127.0.0.1 aming.com [root@akuilinux01 aming.com]# curl https://aming.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文件,使用浏览器测试
# 扩展
- [针对请求的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)
转载于:https://blog.51cto.com/akui2521/2130451