一负载均衡
负载均衡在服务端开发中算是一个比较重要的特性因为 Nginx 除了作为常规的 web 服务器外, 还会被大规模的用于反向代理前端, 因为 Nginx 的异步框架可以处理很大的并发请求, 把这些并发请求 hold 住之后就可以分发给后台服务端 (backend servers, 也叫做服务池, 后面简称 backend) 来做复杂的计算处理和响应, 这种模式的好处是相当多的: 隐藏业务主机更安全, 节约了公网 IP 地址, 并且在业务量增加的时候可以方便地扩容后台服务器
负载均衡可以分为硬件负载均衡和软件负载均衡, 前者一般是专用的软件和硬件相结合的设备, 设备商会提供完整成熟的解决方案, 通常也会更加昂贵软件的复杂均衡以 Nginx 占据绝大多数, 本文也是基于其手册做相应的学习研究的
1 修改虚拟主机配置文件(以 qq.com 为例)
- [root@zlinux ~]# cd /usr/local/nginx/conf/vhost/
- [root@zlinux vhost]# dig qq.com //dig 命令获取 IP, 没有 dig 命令, 使用 yum install -y bind-untils 安装
- ; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.2 <<>> qq.com
- ;; global options: +cmd
- ;; Got answer:
- ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 38970
- ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
- ;; OPT PSEUDOSECTION:
- ; EDNS: version: 0, flags:; udp: 4096
- ;; QUESTION SECTION:
- ;qq.com. IN A
- ;; ANSWER SECTION:
- qq.com. 414 IN A 125.39.240.113
- qq.com. 414 IN A 61.135.157.156
- ;; Query time: 37 msec
- ;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 五 3 月 16 22:00:18 CST 2018
- ;; MSG SIZE rcvd: 67
- // 可以看到两个 IP, 有两个 IP 就可以走负载均衡了
- [root@zlinux vhost]# vim load.conf // 编辑配置文件, 增加以下内容
- # 配置内容
- upstream qq
- # 名字自定义
- {
- ip_hash;
- # 目的: 同一个用户保持在同一个服务器上
- # 即当域名指向多个 IP 时, 保证每个用户始终解析到同一 IP
- server 61.135.157.156:80;
- server 125.39.240.113:80;
- # 指定 web 服务器的 IP
- }
- server
- {
- listen 80;
- server_name www.qq.com;
- location /
- {
- proxy_pass http://qq;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- }
- }
2 测试
- [root@zlinux vhost]# /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@zlinux vhost]# /usr/local/nginx/sbin/nginx -s reload
- [root@zlinux vhost]# curl -x127.0.0.1:80 www.qq.com -I
- HTTP/1.1 200 OK
- Server: nginx/1.12.2
- Date: Fri, 16 Mar 2018 14:18:04 GMT
- Content-Type: text/html; charset=GB2312
- Connection: keep-alive
- Vary: Accept-Encoding
- Vary: Accept-Encoding
- Expires: Fri, 16 Mar 2018 14:19:04 GMT
- Cache-Control: max-age=60
- Vary: Accept-Encoding
- Vary: Accept-Encoding
- X-Cache: HIT from tianjin.qq.com
- // 这里如果不加 - I 选项也是 200 状态码, 因为有默认虚拟主机, 不过其他提示不一样
测试下不加 - I 选项
[root@zlinux vhost]# curl -x127.0.0.1:80 www.qq.com
结果如下图:
注意: Nginx 不支持代理 https, 只能代理 http
二 Nginx 配置 SSL
SSL(Secure Sockets Layer 安全套接层)协议, 及其继任者 TLS(Transport Layer Security 传输层安全)协议, 是为网络通信提供安全及数据完整性的一种安全协议
1 浏览器发送一个 https 的请求给服务器;
2 服务器要有一套数字证书, 可以自己制作(后面的操作就是用自己制作的证书), 也可以向组织申请, 区别就是自己颁发的证书需要客户端验证通过, 才可以继续访问, 而使用受信任的公司申请的证书则不会弹出提示页面, 这套证书其实就是一对公钥和私钥;
3 服务器会把公钥传输给客户端;
4 客户端 (浏览器) 收到公钥后, 会验证其是否合法有效, 无效会有警告提醒, 有效则会生成一串随机数, 并用收到的公钥加密;
5 客户端把加密后的随机字符串传输给服务器;
6 服务器收到加密随机字符串后, 先用私钥解密 (公钥加密, 私钥解密), 获取到这一串随机数后, 再用这串随机字符串加密传输的数据(该加密为对称加密, 所谓对称加密, 就是将数据和私钥也就是这个随机字符串> 通过某种算法混合在一起, 这样除非知道私钥, 否则无法获取数据内容);
7 服务器把加密后的数据传输给客户端;
8 客户端收到数据 (服务端公钥加密) 后, 再用自己的私钥也就是那个随机字符串解密;
1 生成自定义的 SSL 证书(仅坐试验用)
- [root@zlinux conf]# openssl genrsa -des3 -out tmp.key 2048 // 没有 openssl 命令, 则通过 yum install -y openssl 安装
- 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 即私钥, 2048 为加密字符长度, 会让我们输入密码, 不能太短, 否者不成功
- [root@zlinux conf]# openssl rsa -in tmp.key -out zlinux.key
- Enter pass phrase for tmp.key:
- writing RSA key
- // 把 tmp.key 转化成 zlinux.key, 目的是删除刚才设置的密码, 如果不清除密码, 后面很不方便
- [root@zlinux conf]# rm -f tmp.key
- [root@zlinux conf]# openssl req -new -key zlinux.key -out zlinux.csr
- You are about to be asked to enter information that will be incorporated
- into your certificate request.
- What you are about to enter is what is called a Distinguished Name or a DN.
- There are quite a few fields but you can leave some blank
- For some fields there will be a default value,
- If you enter ., the field will be left blank.
- -----
- Country Name (2 letter code) [XX]:CN
- State or Province Name (full name) []:JS
- Locality Name (eg, city) [Default City]:SZ
- Organization Name (eg, company) [Default Company Ltd]:XXLtd
- Organizational Unit Name (eg, section) []:zlinux.com
- Common Name (eg, your name or your servers hostname) []:ZZ
- Email Address []:a@a.com
- Please enter the following extra attributes
- to be sent with your certificate request
- A challenge password []:zzz123456
- An optional company name []:z
- // 生成证书请求文件, key 文件和 csr 文件生成最终的公钥文件 Common Name 为后面配置 Nginx 配置文件 server_name
- [root@zlinux conf]# openssl x509 -req -days 365 -in zlinux.csr -signkey zlinux.key -out zlinux.crt
- Signature ok
- subject=/C=CN/ST=JS/L=C/O=C/OU=C/CN=zlinux.com/emailAddress=z
- Getting Private key
- [root@zlinux conf]# ls |grep zlinux
- zlinux.crt
- zlinux.csr
- zlinux.key
- // 最终生成 crt 证书, 也就是公钥
2 配置 Nginx 支持 SSL
1)编辑配置文件
- [root@zlinux vhost]# vim ssl.conf // 写入以下内容
- server
- {
- listen 443;
- server_name zlinux.com;
- index index.html index.php;
- root /data/wwwroot/ssltest;
- ssl on;
- ssl_certificate zlinux.crt;
- ssl_certificate_key zlinux.key;
- ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
- }
2)检查配置是否有问题
- [root@zlinux vhost]# /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
这说明当前 Nginx 并不支持 SSL, 因为之前 Nginx 编译时并没有配置支持 SSL 的参数, 所以需要重新编译一次, 加上 SSL 参数:
- [root@zlinux vhost]# cd /usr/local/src/nginx-1.12.2
- [root@zlinux nginx-1.12.2]# ./configure --help |grep -i ssl
- --with-http_ssl_module enable ngx_http_ssl_module
- --with-mail_ssl_module enable ngx_mail_ssl_module
- --with-stream_ssl_module enable ngx_stream_ssl_module
- --with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
- --with-openssl=DIR set path to OpenSSL library sources
- --with-openssl-opt=OPTIONS set additional build options for OpenSSL
- [root@zlinux nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
- [root@zlinux nginx-1.12.2]# make
- [root@zlinux nginx-1.12.2]#make install
- [root@zlinux nginx-1.12.2]# /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@zlinux nginx-1.12.2]# /etc/init.d/nginx restart
3)测试
在 window 的 hosts 文件中添加:
- 192.168.242.128 zlinux.com
- [root@zlinux vhost]# mkdir /data/wwwroot/ssltest
- [root@zlinux vhost]# echo "ssl test"> /data/wwwroot/ssltest/index.html
在浏览器中输入 https://zlinux.com, 显示如下图:
来源: http://www.bubuko.com/infodetail-2529520.html