Today for the first time with nginx load balancing, found PHP can not get http_host
The attached nginx configuration
Upstream ABC. com { 10.141.8.55:8005; 10.141.8.55:8006;} server { a; server_name www. xxx. com; Log_not_found off; Access_log /var/log/nginx/html-access. Log main; CharSet UTF-8; / { proxy_pass http://abc.com; }}
echo $_server[' Http_host '];
Output abc.com
Carefully look at the Nginx configuration, it is easy to find that PHP obtained http_host is the Nginx agent came over.
There are two ways to solve this problem.
Method One, change abc.com to you www.xxx.com
But this method is not very good, if server_name have a number of what to do?
So
Method Two
In location/{} plus proxy_set_header Host $host;
Since Http_host has a problem, the other client's information is certainly wrong. such as client IP.
So we have to add other configurations, the complete Nginx configuration is as follows
Upstream ABC.com {server10.141.8.55:8005; Server10.141.8.55:8006;} server {Listen80; server_name www.xxx.com; Log_not_found off; Access_log/var/Log/nginx/html-access.Logmain; 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; }}
Nginx reverse proxy causes PHP not to get the correct http_host,server_name, the solution of client IP