最近,因为要更换云服务器,但是不能更换解析的服务器,于是可以使用NGINX反向代理。
服务器数目两台
- 假解析的域名为:www.testt.com
- 解析的代理服务器A:106.13.*.*
- 被代理服务器B(真实访问的服务器):139.159.*.*
A代理服务器的配置
# 用upstreamp设置代理参数
# /usr/local/nginx/conf/nginx.conf
upstream server_hw_pool {
server 139.159.*.*; #被代理的服务器的IP
}
# 设置解析的域名的配置
# /usr/local/nginx/conf/vhost/www.testtt.com
location / { #作用NGINX服务进行重定向
proxy_pass http://server_hw_pool; #重定向代理,和上面的upstreamp设置参数必须保持一致
proxy_set_header Host $host; #把原请求http请求的Header中的Host字段也放到转发的请求里。
proxy_set_header X-Real-IP $remote_addr;#把用户真实的ip地址转发给后端服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#此处的图片,样式,JS脚本文件缓存配置,都需要隐藏,否则上面的代码,会导致这些静态资源无法加载
#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#{
#expires 30d;
#}
#location ~ .*\.(js|css)?$
#{
#expires 12h;
#}
B服务器的配置
server
{
listen 80;
#listen [::]:80;
server_name www.testtt.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/www.testtt.com;
include other.conf;
#error_page 404 /404.html;
error_page 502 /502set/502.html;
.
. #其他一些参数
.
}
至此,配置完成,当访问A服务器时,会自动转发到B服务器。
负载均衡也是同样的原理。