简体   繁体   中英

With Nginx, how do I preserve the client's IP address when using proxy_pass?

I have a simple proxy_pass in NGINX that looks like this:

location / {
    proxy_pass https://foo.bar/API/;
    rewrite ^/API/(.*) /$1;
}

When I print $_SERVER["REMOTE_ADDR"]; (PHP) on the final destination (reached after the redirect) I see the server's IP instead of the client's IP.

I tried to add

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE_ADDR $remote_addr;

to the location / in order to preserve the client's IP, but that doesn't work (I keep seeing the server's own IP).

How can I preserve the client's IP after a proxy_pass ?

This is a duplicate of How to pass the Remote IP to a proxied service in Nginx .

Also, Nginx questions are more appropriate on serverfault.com.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM