将$ REMOTE_ADDR设置为真实的客户端IP

我正在用Nginx在Heroku上托pipe一个WordPress站点 。 DNS在CloudFlare后面。

我的WordPress插件报告Heroku的IP而不是真正的客户IP。

我有以下的Nginx规则:

location / { try_files $uri $uri/ /index.php?$args; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $http_cf_connecting_ip; } # Allow for internal Heroku router - 10.xxx set_real_ip_from 10.0.0.0/24; # Allow for external CloudFlare proxies - https://www.cloudflare.com/ips set_real_ip_from 199.27.128.0/21; set_real_ip_from 173.245.48.0/20; set_real_ip_from 103.21.244.0/22; set_real_ip_from 103.22.200.0/22; set_real_ip_from 103.31.4.0/22; set_real_ip_from 141.101.64.0/18; set_real_ip_from 108.162.192.0/18; set_real_ip_from 190.93.240.0/20; set_real_ip_from 188.114.96.0/20; set_real_ip_from 197.234.240.0/22; set_real_ip_from 198.41.128.0/17; set_real_ip_from 162.158.0.0/15; set_real_ip_from 104.16.0.0/12; # Recursively process X-Forwarded-For header real_ip_recursive on; real_ip_header X-Forwarded-For; 

完整的configuration可以在https://github.com/wphuman/heroku-buildpack-php/tree/master/conf/nginxfind

每当WordPress插件尝试读取远程地址头$_SERVER['REMOTE_ADDR'] ,他们读取一个10.xxx Heroku IP。

有没有办法将REMOTE_ADDR设置为真实的客户IP?

谢谢

只需添加这一行:

 proxy_set_header REMOTE_ADDR $remote_addr;