配置nginx为php-fpm合并多个X-Forwarded-For
最近用阿里云的容器服务遇到一个问题,docker里的PHP程序用X-Forwarded-For
取用户IP的时候,只能取到SLB的地址。经tcpdump抓包确认,是请求经过阿里云的acsrouting的时候,在HTTP请求头中加入了多个X-Forwarded-For
。查了一下,阿里云用HAProxy做负载均衡服务,又搜了一下HAProxy的资料,发现许多人在用HAProxy时遇到了同类问题,有人给HAProxy提了issue,但对方答复是根据RFC2616,多个同名的header和单个逗号分隔列表构成的header是等价的:
Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.
所以HAProxy不处理已有的X-Forwarded-For
,只是简单地在header末尾加入一个新的X-Forwarded-For
。可惜php-fpm对header的处理方式是同名的header只保留最后一个,所以无法取得正确的用户IP。
我找到一个解决方案是让nginx来合并多个X-Forwarded-For
记录。在docker内的nginx配置中加入以下选项(一般在location ~ \.php$
部分或者在fastcgi_params
配置文件里):
fastcgi_param HTTP_X_FORWARDED_FOR $http_x_forwarded_for if_not_empty;
配置好之后docker内的nginx服务器就会预先合并多个X-Forwarded-For
请求header记录为逗号分隔格式,然后再传给php-fpm。