我有下面的例子
location / { proxy_read_timeout 2000; proxy_next_upstream error; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_pass_header X_CUSTOM_HEADER; proxy_redirect off; proxy_max_temp_file_size 0; proxy_pass https://prod; break; }
现在,当我使用下面的curl线
curl --head -H "X_CUSTOM_HEADER: foo" http://domain.com/api/test
现在,这是行不通的..在后端的Apache / PHP没有看到头。 如果我绕过nginx它的作品
curl --head -H "X_CUSTOM_HEADER: foo" http://web1.domain.com/api/test
你应该underscores_in_headers on默认closures的指令上使用underscores_in_headers on 。
您应该使用proxy_set_header为您希望转发到后端服务器的所有标头。 所以,而不是proxy_pass_header ...行:
proxy_set_header X_CUSTOM_HEADER $http_x_custom_header;
以上不适合我,所以我用了proxy_pass_header 。 在这里查看关于proxy_pass_header的Nginx Wiki 。
如果你的自定义头文件是device_id add proxy_pass_header device_id; 到您的代理块。
如果你正在使用带有下划线的自定义头(像我一样),一定要确保你的Nginxconfiguration中有underscores_in_headers on _in_headers。