一、前提
1:系统linux(centos)
2:nginx代理服务器(web:192.168.1.10 proxy.abc.com)
3:nginx后台服务器(web1:192.168.1.11 www.abc.com web2:192.168.1.12 backend.abc.com)
二、配置(192.168.1.10)
1:配置/usr/local/nginx/config/nginx.conf
将server{}删除掉,通过include config.d/*.conf 引入所有的server配置
在nginx.conf最后添加以下几行:
upstream abc{
#server 127.0.0.1:; ###通过IP##
#server 127.0.0.1:;
server www.abc.com:8000; ###通过域名###
server backend.abc.com:8001;
}
include conf.d/*.conf;
2:配置
/usr/local/nginx/config/config.d/proxy.conf
server {
listen ;
server_name proxy.xiaohuideng.com; #charset koi8-r;
#access_log logs/host.access.log main;
location / {
index index.html index.htm; proxy_set_header Host $http_host; ###默认是以IP为负载(proxy_set_header Host $host;)
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr; ##不过滤掉真实请求IP地址 proxy_pass http://abc; ###引入upstream所定义的名字
} error_page /50x.html;
location = /50x.html {
}
}
三、重启nginx (192.168.1.10)
/usr/local/nginx/sbin/nginx -t ##测试配置是否OK
/usr/local/nginx/sbin/nginx -s reload ###重新加载配置
四、查看效果
在远程的PC机浏览器*问 http://proxy.abc.com看是不是得到backend.abc.com与www.abc.com的页面切换