Home  >  Article  >  Operation and Maintenance  >  How to solve the problem of being unable to obtain the real IP address of the client after Nginx reverse proxy

How to solve the problem of being unable to obtain the real IP address of the client after Nginx reverse proxy

PHPz
PHPzforward
2023-05-14 09:58:051794browse

When we use the Nginx proxy forwarding service, we will find that we cannot obtain the client's real IP address, and therefore cannot obtain the client's geographical location and other information.

1. The original configuration file is as follows

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

2. After configuring forwarding

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        
        # 代理转发
        location /api/{
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Public-Network-URL http://$http_host$request_uri;
            proxy_pass http://localhost:8080/;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

In this way, we forward the client's header information together to obtain the user's Real IP address.

The above is the detailed content of How to solve the problem of being unable to obtain the real IP address of the client after Nginx reverse proxy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete