形如 右边 Â 122.122.122.122 如此的ip地址
nginx配置:
server {
listen 80;
server_name localhost;
location /{
root html;
index index.html index.htm;
proxy_pass http://test;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
}
此时
String ip = request.getHeader("x-forwarded-for");
ip获取是形如 Â 122.122.122.122的地址
解决方式:
server {
listen 80;
server_name localhost;
location /{
root html;
index index.html index.htm;
proxy_pass http://test;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-For $http_x_forwarded_for;
}
即X-real-ip 搭配 $remote_addr;
X-Forwarded-For 搭配 $proxy_add_x_forwarded_for;
不能用$remote_addr变量赋值给X-Forwarded-For
本文探讨了Nginx配置中关于IP地址显示为特殊字符的问题,并提供了具体的解决方案,通过调整proxy_set_header设置,正确处理X-Forwarded-For和X-real-ip头,确保远程地址被正确传递。
1558

被折叠的 条评论
为什么被折叠?



