使用nginx做负载均衡或http代理时,碰到http header不转发的问题。
配置里只有转发设置原始ip和host的
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Server $host;
当然我也感觉非常好奇,用了这么久为什么这就这个问题转发不过去,请求的内容也是正确的。
后来我在nginx.conf把error的日志等级更改为debug,error_log logs/error.log debug;发现读取信息如下:
2017/11/20 14:27:56 [info] 5625#0: *1078154 client sent invalid header line: "access_token: JhbGciOiJSUzI1NiJ9.eyJqdGkiOiJKV1Q6NmJlNmE3ODUtMmNiZS00NTkwLThkYmQtMzI4MTI0ZTRjZDBiIiwiaWF0IjoxNTExMTU3NTkzLCJzdWIiOiJ7XCJjcmVhdGVUaW1lXCI6XCIyMDE3LTA3LTEwIDA5OjU5OjMyXCIsXCJlbWFpbFwiOlwiYWRtaW5AbTJjLmNvbVwiLFwibmFtZVwiOlwiYWRtaW5cIixcIm5vdGVcIjpcIm5vdGVcIixcInBlcm1pc3Npb25LZXlcIjpcIkpXVDpTWVNURU06UEVSTUlTU0lPTpkYmRjMjQ1ZGM5Mjg0MmY3OTAxY2ZjNGZiOWU3ZmY1OVwiLFwicm9sZUlkXCI6XCIxXCIsXCJ0ZWxOb1wiOlwiMTM4Mjg3NDk3NTVcIixcInVwZGF0ZVRpbWVcIjpcIjIwMTctMTEtMTcgMTY6MTY6MTdcIixcInVzZXJJZFwiOlwiNmM3MTFlOGUzZjA1MTIzNDUwZDgzMGI5NGM3MDRjY2E3OTlmXCIsXCJ1c2VyTmFtZVwiOlwiYWRtaW5cIixcInVzZXJOb1wilwiMVwiLFwidXNlclN0YXR1c1wiOlwiTk9STUFMXCJ9IiwiZXhwIjoxNTExMjAwNzkzfQ.ZrUmlI0FSC0FjUwXvReuj4YlHYyCdJrDx9e9B1OEazO3avmMEsGjZoWxxxxxxxAbtqv5sFa64_U0GL-RdE9fkQRWE9k8pSjYs34mOYI1qN_m3ZdbbEGioJ4OrY_IJj3qax5BPP9g9AejhFM9y8Z7zt7SV6YU" while reading client request headers, client: 10.0.40.102, server: api.xxxxxx.com, request: "GET /xxx.operxxxx/role/auth/get/modules?systemAccessToken=JhbGciOiJSUzI1NiJ9.eyJqdGkiOiJKV1Q6NmJlNmE3ODUtMmNiZS00NTkwLThkYmQtMzI4MTI0ZTRjZDBiIiwiaWF0IjoxNTExMTU3NTkzLCJzdWIiOiJ7XCJjcmVhdGVUaW1lXCI6XCIyMDE3LTA3LTEwIDA5OjU5OjMyXCIsXCJlbWFpbFwiOlwiYWRtaW5AbTJjLmNvbVwiLFwibmFtZVwiOlwiYWRtaW5cIixcIm5vdGVcIjpcIm5vdGVcIixcInBlcm1pc3Npb25LZXlcIjpcIkpXVDpTWVNURU06UEVSTUlTU0lPTjpkYmRjMjQ1ZGM5Mjg0MmY3OTAxY2ZjNGZiOWU3ZmY1OVwiLFwicm9sZUlkXCI6XCIxXCIsXCJ0ZWxOb1wiOlwiMTM4Mjg3NDk3NTVcIixcInVwZG0ZVRpbWVcIjpcIjIwMTctMTEtMTcgMTY6MTY6MTdcIixcInVzZXJJZFwiOlwiNmM3MTFlOGUzZjA1MTIzNDUwZDgzMGINGM3MDRjY2E3OTlmXCIsXCJ1c2VyTmFtZVwiOlwiYWRtaW5cIixcInVzZXJOb1wiOlwiMVwiLFwidXNlclN0YXR1c1wiOlwiTk9STUFMXCJ9IiwiZXhwIjoxNTExMjAwNzkzfQ.ZrUmlI0FSC0FjUwXvReuj4YlHYyCdJrDx9e9B1OEazO3avmMEsGjZoWxxxxxxxAbtqv5sFa64_U0GL-RdE9fkQRWE9k8pSjYs34mOYI1qN_m3ZdbbEGioJ4OrY_IJj3qax5BPP9g9AejhFM9y8Z7zt7SV6YU&roleId=SYSRC849xxxC7xx5CA64D349D6A03AAE2C511F4 HTTP/1.1", host: "api.xxxxxxx.com"
提示客户端发送无效的header信息。
原来是对header name的字符做了限制,默认 underscores_in_headers 为off,表示如果header name中包含下划线,则忽略掉。
恰好我自定义的header中都是用的下划线。
处理办法:
1:配置中http部分 增加underscores_in_headers on; 配置
2:用减号-替代下划线符号_,避免这种变态问题。nginx默认忽略掉下划线可能有些原因。
可以加到http或者server中
语法:underscores_in_headers on|off
默认值:off
使用字段:http, server
是否允许在header的字段中带下划线。
参考:
http://www.ttlsa.com/nginx/nginx-proxy_set_header/
http://blog.csdn.net/wx_mdq/article/details/10466891
时间: 2024-10-13 02:47:27