You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx+Laravel环境Proxy Pass保留原域名实现staging访问问题求助

问题根因

内网staging服务器返回了携带自身内网IP的301/302重定向响应,现有Nginx配置默认直接透传该响应给浏览器,导致地址栏跳转至内网IP。

修复方案

补全Nginx重定向地址重写规则与必要代理头,修正后端返回的错误重定向地址,同时告知内网staging服务器原始请求的协议与域名,从根源减少错误重定向的产生。

修改后完整Nginx配置

upstream staging {
        server 192.168.1.100:443;
}

server {
        listen          80;
        server_name     staging-backend.mydomain.com;
        return 301 https://$host$request_uri;
}

server {
        listen          443 ssl;
        server_name     staging-backend.mydomain.com;

        ssl_certificate         /var/www/project/ssl_certs/__mydomain_com.chain.crt;
        ssl_certificate_key     /var/www/project/ssl_certs/__mydomain_com.key;

        access_log      /var/log/nginx/staging_backend.access.log;
        error_log       /var/log/nginx/staging_backend.error.log;

        # 关闭内网HTTPS证书校验,避免内网自签/不信任证书导致502错误
        proxy_ssl_verify off;

        location / {
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header        Host $host;
                # 透传原始请求协议,避免后端错误跳转HTTP地址
                proxy_set_header        X-Forwarded-Proto $scheme;
                # 重写后端返回的内网IP重定向地址为对外域名
                proxy_redirect          https://192.168.1.100/ https://staging-backend.mydomain.com/;
                proxy_redirect          http://192.168.1.100/ https://staging-backend.mydomain.com/;
                
                proxy_pass              https://staging$request_uri;
        }
}

生效操作

配置修改完成后,执行以下命令生效:

  • 检查配置语法正确性:nginx -t
  • 重载Nginx配置:nginx -s reload

如果修改后仍偶发跳转内网IP的问题,可直接修改内网staging服务器的站点配置,将站点默认域名设置为staging-backend.mydomain.com,从源端避免返回内网IP地址。

内容的提问来源于stack exchange,提问作者Lwin Htoo Ko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 16:57:00