nginx反向代理/负载均衡配置,供大家学习参考。
实验环境三台服务器:
一台nginx作为前端反向代理服务器,IP地址192.168.2.73
一台apache作为后端的web服务器(apache用的系统自带的),IP地址192.168.5.54
一台apache作为后端的web服务器(apache用的系统自带的),IP地址192.168.5.57
nginx服务器配置:
1、安装步骤很简单。
2、修改nginx.conf文件,设置proxy相关参数,在httpd字段中增加如下内容:
继续修改nginx.conf文件,在server中lication /配置中做如下修改:
然后保存,检查配置文件是否有问题
[root@hadoop3 ~]# /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
3、启动nginx
[root@hadoop3 ~]# /usr/local/nginx/sbin/nginx
配置两台apache服务器
登录192.168.5.54上操作:
[root@hadoop5 ~]# echo 'this is 192.168.5.54!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@hadoop5 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@hadoop5 ~]# /etc/init.d/httpd start
登录192.168.5.57上操作:
[root@service ~]# echo 'Hello,This is 192.168.5.57!' > /var/www/html/index.html
修改/etc/httpd/conf/httpd.conf文件的监听端口为8080
[root@service ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@service ~]# /etc/init.d/httpd start
测试:
[root@hadoop3 ~]# for i in $(seq 20); do curl http://192.168.2.73/; done
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
this is 192.168.5.54!
Hello,This is 192.168.5.57!
成功完成了nginx反向代理服务器的配置,如果我没记错应该比lvs的负载强一点,lvs只支持80端口转发到80端口,而nginx可以80端口转发到任意不一样的端口。