贴上的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; } }