nginx解码特殊字符引发400问题处理案例分享( 二 )


  • 8.如何避免这个问题?
正常情况下,uri的转义操作在浏览器端已经完成,所以nginx侧的$request_uri肯定是encode后的正确状态,这一点在文章开头中Kibana的access日志中可以看到 。我们可以利用这个参数,对location进行特殊处理
修改前的配置如下
location / {proxy_set_header Host $http_host;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://10.89.0.8/$1;}修改后的配置如下
location / {proxy_set_header Host $http_host;proxy_set_header X-Forwarded-Proto $scheme;proxy_set_header X-Real-IP$remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;if ($request_uri ~* ^/(.*)$) {proxy_pass http://10.89.0.8/$1;}}
nginx解码特殊字符引发400问题处理案例分享

文章插图
 
写在后面
nginx内置变量很多,配合location rewrite正则可以满足多种转发场景 。
有其他解决思路的朋友可以留言哟~ 。




推荐阅读