4 Nginx代理服务器
- 1 Nginx代理服务器
- 2 实现代理功能
- 3 优化配置
- 3.1 解决用户重复登陆的问题
- 3.2 健康检查
- 3.3 权重
- 4 Nginx的四层代理
1 Nginx代理服务器
Nginx除了作为后端HTTP服务器以外,它还是一个高性能的反向代理服务器。
在负载均衡的架构中,Nginx可以为我们提供非常稳定且高效的基于七层的负载均衡解决方案
Nginx可以根据规则以轮询、IP哈希、URL哈希等方式调度后端真实服务器
Nginx也支持对后端服务器的健康检查功能
- 实验环境准备
为web1和web2安装httpd服务
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# systemctl start httpd
[root@web1 ~]# netstat -ntulp | grep 80
为web1和web2分别创建测试页面
[root@web1 ~]# echo web_1 > /var/www/html/index.html
[root@web2 ~]# echo web_2 > /var/www/html/index.html
测试
[root@proxy ~]# curl http://192.168.2.100
web_1
[root@proxy ~]# curl http://192.168.2.200
2 实现代理功能
- 修改配置文件
[root@proxy nginx-1.12.2]# ./configure
[root@proxy nginx-1.12.2]# make
[root@proxy nginx-1.12.2]# make install
[root@proxy ~]# cat -n /usr/local/nginx/conf/nginx.conf | sed -n '34,37p'
34 upstream webserver {
35 server 192.168.2.100:80;
36 server 192.168.2.200:80;
37 }
#使用upstream定义后端服务器集群,集群名称任意(如webserver)
#使用server定义集群中的具体服务器和端口
[root@proxy ~]# cat -n /usr/local/nginx/conf/nginx.conf | sed -n '46,50p'
46 location / {
47 proxy_pass http://webserver;
#通过proxy_pass将用户的请求转发给webserver集群
48 root html;
49 index index.html index.htm;
50 }
[root@proxy ~]# /usr/local/nginx/sbin/nginx
- 测试
- 默认是轮询的方式
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
web_2
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
3 优化配置
3.1 解决用户重复登陆的问题
[root@proxy ~]# cat -n /usr/local/nginx/conf/nginx.conf | sed -n '34,38p'
34 upstream webserver {
35 ip_hash; #相同客户机访问相同服务器
36 server 192.168.2.100:80;
37 server 192.168.2.200:80;
38 }
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
#再次使用浏览器测试发现不再轮询访问
3.2 健康检查
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
[root@proxy ~]# cat -n /usr/local/nginx/conf/nginx.conf | sed -n '34,38p'
34 upstream webserver {
35 #ip_hash;
36 server 192.168.2.100:80 max_fails=2 fail_timeout=20;
37 server 192.168.2.200:80 max_fails=2 fail_timeout=20;
38 }
//max_fails可以定义检测失败多少次就不再轮询该主机
//fail_timeout可以定义多少秒检测主机的间隔时间
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
- 测试
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
web_2
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
^C
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
^C
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
^C
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
3.3 权重
可以决定集群中主机被轮询的比率,性能较强的主机可以提高权重值
[root@proxy ~]# cat -n /usr/local/nginx/conf/nginx.conf | sed -n '34,38p'
34 upstream webserver {
35 #ip_hash;
36 server 192.168.2.100:80 weight=1 max_fails=2 fail_timeout=20;
37 server 192.168.2.200:80 weight=2 max_fails=2 fail_timeout=20;
38 }
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s reload
- 测试
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
web_2
[root@proxy ~]# curl http://192.168.2.5
web_2
[root@proxy ~]# curl http://192.168.2.5
web_1
[root@proxy ~]# curl http://192.168.2.5
4 Nginx的四层代理
- 重新安装nginx
[root@proxy ~]# /usr/local/nginx/sbin/nginx -s stop
[root@proxy ~]# rm -rf /usr/local/nginx/
[root@proxy opt]# cd nginx-1.12.2/
[root@proxy nginx-1.12.2]# ls
auto CHANGES.ru configure html Makefile objs src
CHANGES conf contrib LICENSE man README
[root@proxy nginx-1.12.2]# ./configure --with-stream --with-http_stub_status_module --with-http_ssl_module
//--with-stream 开启四层代理的模块
//--with-http_stub_status_module 可以查看nginx网站后台数据的模块
[root@proxy nginx-1.12.2]# make
[root@proxy nginx-1.12.2]# make install
[root@proxy nginx-1.12.2]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
- 修改配置
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
[root@proxy ~]# cat -n /usr/local/nginx/conf/nginx.conf | sed -n '16,25p'
16 stream {
17 upstream backend { //定义集群
18 server 192.168.2.100:22; //集群中的主机以及业务端口号
19 server 192.168.2.200:22;
20 }
21 server { //调用集群
22 listen 12345; //监听端口号,可以自定义
23 proxy_pass backend; //调用后台集群的名称
24 }
25 }
[root@proxy ~]# /usr/local/nginx/sbin/nginx
- 查看12345端口
[root@proxy ~]# netstat -ntulp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4329/ngin: master
tcp 0 0 0.0.0.0:12345 0.0.0.0:* LISTEN 4329/ngin: master
- 测试
[c:\~]$ ssh 192.168.2.5 12345
Last login: Wed Jul 15 03:19:32 2020 from 192.168.2.1
[root@web2 ~]# logout
[c:\~]$ ssh 192.168.2.5 12345
Last login: Wed Jul 15 03:19:28 2020 from 192.168.2.1
[root@web1 ~]#