貼上的Nginx配置
upstream abc.com {
server 10.141.8.55:8005;
server 10.141.8.55:8006;
}
server {
listen 80;
server_name www.xxx.com;
log_not_found off;
access_log /var/log/nginx/html-access.log main;
charset utf-8;
location / {
proxy_pass http://abc.com;
}
}
echo $_SERVER['HTTP_HOST'];
輸出abc.com
仔細查看Nginx配置,很容易發現PHP獲取到的HTTP_HOST是Nginx代理過來的。
解決的辦法有兩種
方法一,將abc.com改成你www.xxx.com
不過這種方法還不是很好,如果server_name有多個怎么辦呢
所以
方法二
在location/{}加上 proxy_set_header Host $host;
既然HTTP_HOST有問題,那其他客戶端的信息也肯定是不對的。比如客戶端IP。
所以還得加上其他配置,完整的Nginx配置如下
upstream abc.com {
server 10.141.8.55:8005;
server 10.141.8.55:8006;
}
server {
listen 80;
server_name www.xxx.com;
log_not_found off;
access_log /var/log/nginx/html-access.log main;
charset utf-8;
location / {
proxy_pass http://abc.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header HTTP_X_FORWARDED_FOR $remote_addr;
proxy_redirect default;
}
}
