Nginx in the process of reverse proxy, real User IP acquisition __nginx

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx reverse proxy is a common function of the process of Web layout. After setting the reverse proxy, by reading the HTTP header inside the REMOTE_ADDR can not get the true user IP, we need to increase the nginx configuration to solve this problem.

The following is an experiment to illustrate.

Configure 1 Nginx servers and configure 3 domain names: t2.guokai.work, Tt2.guokai.work, Ttt2.guokai.work.

In the Nginx configuration, when accessing T2.guokai.work, the agent forwards to the Tt2.guokai.work, which is then forwarded to Ttt2.guokai.work by the tt2.guokai.work agent.

In the configuration of T2.guokai.work and Tt2.guokai.work, a key configuration needs to be added:

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
# Proxy_pass Http://tt2.guokai.work; #//t2.guokai.work configuration
proxy_pass http://ttt2.guokai.work; #//Tt2.guokai.work Configuration

Where the Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for refers to the x-forwarded-for that adds "To whom to forward" append to the HTTP header during the forwarding process. Each time forwarding, as long as there is this sentence configuration, will be the previous IP write to the X-forwarded-for field, if there is already this field, then separated by commas.

Look at the effect of the test:

ttt2.guokai.work/index.php
foreach ($_server as $key => $val) {
    echo $key. " ---> ". $val." <br/> ";
}

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.