日安,
我的问题是,当使用Laravel的或$request->getClientIp ()
或$request->server->get('REMOTE_ADDR')
或$_SERVER['REMOTE_ADDR']
时,我只能获得Docker地址,这基本上都是我假设的。到目前为止,这个问题发生在我的本地测试pc (没有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
我已经读了好几个小时的论坛和解决方案,尝试和改变吐露等,但似乎都没有效果。
我尝试过的:
因此,我还试图在Laravel的/Http/Middleware/TrustProxies.php
中设置我的“可信代理”,方法是允许all: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;
}
}
发布于 2021-03-12 12:32:05
好的,想清楚了!
HTTP_CF_CONNECTING_IP
“注入”到我的头上。要检索此标头值,我在代码中使用了$_SERVER['HTTP_CF_CONNECTING_IP']
._SERVER['HTTP_X_FORWARDED_FOR']
从标题中使用HTTP_X_FORWARDED_FOR
值,在我的情况下,这只会在执行以下操作(在我的问题中发布)之后才能工作:我希望这对其他人也有帮助。
https://stackoverflow.com/questions/66375236
复制相似问题