简体   繁体   English

如何在 Docker 中使用 NGINX 获取远程 IP / 客户端 IP? 同时使用 Laravel

[英]How can I get the remote IP / Client IP using NGINX in Docker ?? Also using Laravel

I am running NGINX as part of a Docker package. It is both a webserver and a reverse proxy, and the container has PHP bundled in with it.我正在运行 NGINX 作为 Docker package 的一部分。它既是网络服务器又是反向代理,容器中捆绑了 PHP。 The front-end web application is built with Laravel. There are some instances where I want to get the client's IP address, and this seems to a little problematic in some cases.前端 web 应用程序是用 Laravel 构建的。有些情况下我想获取客户端的 IP 地址,这在某些情况下似乎有点问题。 This isn't for the proxy, but for the web application served with NGINX and PHP.这不是针对代理,而是针对与 NGINX 和 PHP 一起服务的 web 应用程序。

On my development system at home I am getting an IP for the docker.network when connecting with a browser from my local machine.在我家里的开发系统上,当从我的本地机器连接浏览器时,我得到了 docker.network 的 IP。

$_SERVER['SERVER_ADDR'] = 172.19.0.10
$_SERVER['REMOTE_ADDR'] = 172.19.0.1

On a Digital Ocean Dev server it has:在 Digital Ocean Dev 服务器上,它有:

$_SERVER['SERVER_ADDR'] 172.27.0.16
$_SERVER['REMOTE_ADDR'] 213.225.x.xx, which is Austria, my IP

and on a Production server elsewhere I think it has:在其他地方的生产服务器上,我认为它有:

$_SERVER['SERVER_ADDR'] ???
$_SERVER['REMOTE_ADDR'] = The WAN IP for the office where the server is installed.

This isn't the client IP, but the WAN address for the office itself.这不是客户端 IP,而是办公室本身的 WAN 地址。

The server at that office is behind I think a WatchGuard or Fortigate router / Firewall.那个办公室的服务器在后面,我认为是 WatchGuard 或 Fortigate 路由器/防火墙。

So, the Digital Ocean Dev server is actually "OK".所以,Digital Ocean Dev 服务器实际上是“OK”的。 I have access to what I need, but the office setup probably isn't setup to forward the client ip to the server that runs on their inte.net?我可以访问我需要的东西,但办公室设置可能未设置为将客户端 ip 转发到在其 inte.net 上运行的服务器?

It isnt' critical currently, but it would be nice to be able to capture to public client IP in all cases.目前这不是关键,但如果能够在所有情况下都捕获到公共客户端 IP 就更好了。

I could do a little more investigation with log files, etc., but pretty sure I'll have to:我可以对日志文件等做更多的调查,但很确定我必须:

  1. Possibly configure something in my NGINX config可能在我的 NGINX 配置中配置一些东西

and/or和/或

  1. Have someone configure the firewall to pass through the client IP because it seems like it isn't doing that currently, or I'm not capturing what it is sending.让某人将防火墙配置为通过客户端 IP,因为它目前似乎没有这样做,或者我没有捕获它发送的内容。

The nginx proxy at the office can be configured to pass the client's IP address using the proxy_set_header directive.办公室的 nginx 代理可以配置为使用proxy_set_header指令传递客户端的 IP 地址。

The nginx reverse proxy docs here show an example:此处的 nginx 反向代理文档显示了一个示例:

location /some/path/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_pass http://localhost:8000;
}

In the config block where the reverse proxy proxy_pass directive is set, adding proxy_set_header X-Real-IP $remote_addr;在设置反向代理proxy_pass指令的配置块中,添加proxy_set_header X-Real-IP $remote_addr; tells the proxy to inject a new HTTP header, X-Real-IP , with the IP address of the client.告诉代理注入一个新的 HTTP header, X-Real-IP ,以及客户端的 IP 地址。 In the Laravel app, you can use this to get the actual client IP. In your application, make sure only to use this header when it can be trusted 1 .在 Laravel 应用程序中,您可以使用它来获取实际的客户端 IP。在您的应用程序中,请确保仅在可信任时使用此 header 1

This is necessary because the proxy is taking and re-sending the client's request via the proxy server from its own.network address.这是必要的,因为代理正在通过代理服务器从其自己的网络地址接收并重新发送客户端的请求。 The only way the application can get the original client IP for the request being proxied is if that IP address is passed in the headers by the proxy.应用程序可以为被代理的请求获取原始客户端 IP 的唯一方法是代理在标头中传递该 IP 地址。

1 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#security_and_privacy_concerns 1 https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For#security_and_privacy_concerns

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

相关问题 如何在docker中使用symfony从请求中获取客户端IP - How to get client ip from request using symfony in docker 如何使用php获取客户端的本地IP? - How to get the local IP of the client using php? 使用Nginx,使用proxy_pass时如何保存客户端的IP地址? - With Nginx, how do I preserve the client's IP address when using proxy_pass? Docker Swarm 在 Nginx 中获取真实 IP(客户端主机) - Docker Swarm get real IP (client host) in Nginx 如何使用ip地址“request() - > ip();”获取货币区域设置 - how can i get currency locale using ip address “ request()->ip();” 如何在 Laravel 5+ 中获取客户端 IP 地址 - How to get client IP address in Laravel 5+ 如何使用Laravel 5.2在雄辩的create方法中自动将Request()-> ip()中的客户端IP地址包括在内? - How to automatically include client ip address from Request()->ip() in eloquent create method using Laravel 5.2? 如何获取客户端的IP地址? - How can I get client's IP address? 如何使用 PHP 获取客户端的 IP 地址和端口? - How can I get the IP address and port of a client with PHP? 如何在 PHP 中获取已连接客户端的 MAC 和 IP 地址? - How can I get the MAC and the IP address of a connected client in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM