最近,因爲要更換雲服務器,但是不能更換解析的服務器,於是可以使用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服務器。
負載均衡也是同樣的原理。