Nginx Configure the virtual host domain name mingzi.111cn.net, when the viewer visits the main station, through the proxy to visit server1.mingzi.111cn.net website. So as to achieve a load balancing effect.
The following is the Nginx load balancing, reverse proxy configuration:
| code is as follows |
copy code |
|
Server {
server_name mingzi.111cn.net;
Listen;
location/{
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 Via "Nginx";
Proxy_pass http://server1.mingzi.111cn.net:80;
}
} |
Set header information through Proxy_set_header, including host name (hostname), X-REAL-IP (real IP), sender.
Real information before PHP gets the proxy
You can get the information before the agent by printing the PHP $_server.
1. Acquire host name (host domain name)
[Http_host] => mingzi.111cn.net
If $host without Proxy_set_header Host; Set, its value is server1.mingzi.111cn.net.
2. Get the real IP address of the visitor
| The code is as follows |
Copy Code |
|
[Http_x_real_ip] => 58.220.197.34
[Http_x_forwarded_for] => 58.220.197.34 |
If the Proxy_set_header X-real-ip and X-forwarded-for are set, its value is server1.mingzi.111cn.net IP address, which is very bad.
The following is the IP address function that gets the real user:
| The code is as follows |
Copy Code |
function Getclientip ($checkProxy = True)
{
if ($checkProxy && @$_server[' http_client_ip ']!= null) {
$ip = @$_server[' http_client_ip '];
}
else {
if ($checkProxy && @$_server[' http_x_forwarded_for ']!= null) {
$ip = @$_server[' http_x_forwarded_for '];
} else {
$ip = @$_server[' remote_addr '];
}
}
return $IP;
}
|
3. Get Sender Header information
[Http_via] => Nginx
If Proxy_set_header Via "Nginx" is not available, this value cannot be obtained on the proxy host.
The reverse proxy that Nginx does when doing load balancing has a disadvantage that it cannot pass cookies.