大网数据广告活动
网站运营与故障问答

Get real user IP behind two reverse proxies

· 52CE 我爱测

This is my environment setup: Server A hosts the site (Nginx + PHP-FPM + Wordpress) Server B is Nginx reverse proxy for server A Cloudflare reverse proxy is on top of all. Therefore, user request first go to Cloudflare, then serv…

问题原文

原帖链接 · Ali Hashemi · CC BY-SA 3.0

This is my environment setup:

  • Server A hosts the site (Nginx + PHP-FPM + Wordpress)
  • Server B is Nginx reverse proxy for server A
  • Cloudflare reverse proxy is on top of all.

Therefore, user request first go to Cloudflare, then server B and then server A. I've set:

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

on server B. I've also installed Cloudflare wordpress plugin, but Wordpress still gets server B IP address instead of user IP address, detecting Cloudflare IP addresses as originating IPs. :(

Installing Proxy Real IP changes Server B IP addresses to Cloudflare IP addresses, not real user IP.

Any help to get real user IP address at Wordpress end?

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 3.0

When multiple proxies are involved and they all support X-Forwarded-For, they each append a value to it. Cloudflare states this in its documentation.

To get this behavior from nginx it looks like you need to set the real_ip_recursive option.

So on the WordPress side the real IP address of the user will usually be the first IP address in the comma separated value list of the X-Forwarded-For header.

Your WordPress header states that it uses "a regular expression" to match the IP address from that field, but it doesn't state that it can deal with multiple values in that field. You may need to update the code of the plugin to work properly when there are multiple values.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-28)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。