Nginx reverse proxy causes PHP not to get the correct http_host,server_name, the solution of client IP

Source: Internet
Author: User
Tags nginx reverse proxy nginx load balancing

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

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.