nginx 端口转发后 反向代理配置 访问域名错误

浏览:1233次阅读
没有评论

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
正文完
 0
包子
版权声明:本站原创文章,由 包子 2021-10-27发表,共计662字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)