简体   繁体   English

使用Nginx,使用proxy_pass时如何保存客户端的IP地址?

[英]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: 我在NGINX中有一个简单的proxy_pass ,如下所示:

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

When I print $_SERVER["REMOTE_ADDR"]; 当我打印$_SERVER["REMOTE_ADDR"]; (PHP) on the final destination (reached after the redirect) I see the server's IP instead of the client's IP. (PHP)在最终目标(重定向后到达)上,我看到的是服务器的IP,而不是客户端的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). location /以保留客户端的IP,但这是行不通的(我一直看到服务器自己的IP)。

How can I preserve the client's IP after a proxy_pass ? proxy_pass之后如何保存客户端的IP?

This is a duplicate of How to pass the Remote IP to a proxied service in Nginx . 这与如何将远程IP传递到Nginx中的代理服务重复。

Also, Nginx questions are more appropriate on serverfault.com. 同样,Nginx问题在serverfault.com上更合适。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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