内容目录
nginx 端口转发后 反向代理配置 访问域名错误
应用场景
我们的 nginx 外网端口映射为
192.168.1.1:8080(nginx)=>0.0.0.0:8081
原始配置:
server { | |
listen 8080; | |
server_name test.xxxx.com; | |
location / { | |
proxy_pass http://192.168.1.15:80; | |
index index.html index.htm; | |
} | |
} |
解决方法
# 添加 header 头 | |
proxy_set_header Host test.xxxx.com:8081; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
但是上面的方法是手动写 header 头不太靠谱
修改为以下方案
# 第一版方案 | |
proxy_set_header Host $host:8081; | |
# 最终方案 | |
proxy_set_header Host $http_host; | |
# 注意 $server_port 是 linsten 监听端口 $proxy_prot 是代理接口 我们这个是端口转发后的 所以都不得劲 得用 http_host |
# 错误案例 1 | |
proxy_set_header Host $host:$server_port; | |
# 以上会跳转到 xxx.com:8080/index.html | |
# 错误案例 2 | |
proxy_set_header Host $host:$proxy_port; | |
# 以上会跳转到 xxx.com/index.html |
正文完