【发布时间】:2021-05-28 05:01:51
【问题描述】:
早安,
我的问题是,当我使用 Laravel 的 $request->ip() 或 $request->getClientIp () 或 $request->server->get('REMOTE_ADDR') 或 $_SERVER['REMOTE_ADDR'] 时,我只获取我的 Docker 网络 IP 地址,这基本上都是我假设的。
到目前为止,问题出现在我的本地测试电脑(没有 Nginx 反向代理)以及我的实时服务器上
我希望我在下面提供了足够的相关信息,如果没有,请告诉我:) 提前致谢!
所以我的设置如下(简化),两个 Nginx 配置也粘贴在下面:
Server -> Nginx Reverse Proxy -> Docker Containers & Network (db, php, nginx, redis) -> Nginx Webserver <-> PHP-FPM
与_SERVER相关的一些结果:
[SERVER_ADDR] => 172.20.0.3 (nginx webserver)
[REMOTE_PORT] => 55378
[REMOTE_ADDR] => 172.20.0.1 (docker network / gateway)
[SERVER_SOFTWARE] => nginx/1.19.6
在尝试和更改配置等时,我已经阅读了数小时和数小时的多个论坛和解决方案,但似乎都没有工作。
我尝试过的:
- Get User IP address in laravel with similar method to HTTP_X_FORWARDED_FOR
- https://serverfault.com/questions/618456/getting-the-client-ip-when-passing-through-a-reverse-proxy
所以我也尝试在 Laravel 的 /Http/Middleware/TrustProxies.php 中设置我的“可信代理”,方法是允许所有:protected $proxies = '*';
我也不需要这些额外的 Laravel 包 >5.5 我相信这是 Laravel (TrustProxies) 的内置功能。
请在下面找到我的 Nginx 网络服务器配置:
server {
listen 80;
server_name domain.com;
return 301 https://www.example.com;
}
server {
listen 80 default_server;
index index.html index.htm index.php;
server_name www.example.com;
root /var/www/public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9013;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/example.com/public/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# Support Clean (aka Search Engine Friendly) URLs & enable Gzip compression:
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
# Override the load balancer IP with real IP.
fastcgi_param REMOTE_ADDR $http_x_real_ip;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
}
还有我服务器的 Nginx 反向代理配置(如果相关):
server {
listen 80;
listen [::]:80;
server_name example.com.au www.example.com.au;
location / {
proxy_pass http://localhost:8013;
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 X-Forwarded-Host $server_name;
}
}
【问题讨论】:
-
我刚刚意识到,也许在我的本地 pc/localhost 上也作为服务器运行,这可能是预期的行为??
-
如果在本地主机上这是预期的行为,我仍然很想知道如何在我的实时服务器上实现这一点,如问题中所述
标签: php docker nginx nginx-reverse-proxy laravel-8