Linux in conjunction with PHP implementation Nginx Reverse Proxy

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.