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

Nginx反向代理/重定向失效求助:Docker容器路径转发配置

Nginx反向代理路径映射配置需求

服务器上运行两个Docker容器:

  • 容器1的Nginx监听80端口,接收AWS应用负载均衡器(ALB)的请求,需根据URL路径转发到容器2的3个端口之一
  • 容器2的应用有自有路径规则,要求转发后浏览器URL保持原前缀,不暴露后端端口和内部路径

具体场景要求:

  • 访问https://example.com/story/fairy时,转发到容器2的9091端口
  • 应用返回内容中的路径(如/_myownpath/page1/),需在浏览器显示为https://example.com/story/fairy/_myownpath/page1
  • 页面内其他链接(如/_newpath/story_page_11)点击后,仍转发到9091端口,浏览器URL保持https://example.com/story/fairy/_newpath/story_page_11

尝试过的proxy_pass配置

server {
        listen 80;
        server_name example.com;
    
        location /story/fairy/ {
            # Reject requests with unsupported HTTP method
            if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
                return 405;
            }
    
            # Only requests matching the whitelist expectations will
            # get sent to the application server
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://localhost:9091/;
            proxy_redirect http://localhost:9091/ https://$server_name/;
        }
}

尝试过的重定向配置

server {
    listen 80;

    location /awsmap/dev/ {
        if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
            return 405;
        }

        return 301 http://localhost:9091/;
    }
}

尝试过的rewrite配置

server {
    listen 80;

    location /story/fairy {
        rewrite ^/story(.*)$ / break;

        proxy_pass http://localhost:9091;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;

        absolute_redirect off;
    }

    location ^~ /story/fairy/ {
    rewrite ^/story(.*)$ / break;

        proxy_pass http://localhost:9091;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_cache_bypass $http_upgrade;

        absolute_redirect off;
    }

}

解决方案配置
server {
    listen 80;
    server_name example.com;

    # 匹配所有以/story/fairy开头的请求(带/不带/都覆盖)
    location ^~ /story/fairy {
        # 拒绝不支持的HTTP方法
        if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE)$) {
            return 405;
        }

        # 剥离/story/fairy前缀,将剩余路径转发到后端9091端口
        rewrite ^/story/fairy(.*)$ $1 break;
        proxy_pass http://localhost:9091;

        # 传递必要的代理头,确保后端能正确识别请求来源
        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-Proto $scheme; # 适配ALB的HTTPS转发场景

        # 修正后端返回的重定向URL,替换为带前缀的路径
        proxy_redirect http://localhost:9091/ https://$server_name/story/fairy/;
        proxy_redirect / /story/fairy/;

        # 替换页面内的绝对链接,避免用户点击后跳转到错误路径
        sub_filter_types text/html text/css text/javascript;
        sub_filter '="/' '="/story/fairy/';
        sub_filter 'href="/' 'href="/story/fairy/';
        sub_filter 'src="/' 'src="/story/fairy/';
        sub_filter_once off; # 替换页面中所有匹配的链接
    }
}

配置要点说明

  1. 路径转发规则:rewrite指令将/story/fairy前缀剥离,把剩余路径传递给后端,确保后端收到的路径符合其自有规则
  2. 重定向修正:proxy_redirect将后端返回的根路径重定向替换为带前缀的URL,避免浏览器直接跳转到后端地址
  3. 页面链接替换:sub_filter处理页面内的绝对链接(如href="/xxx"),自动添加/story/fairy前缀,保证用户点击后请求能被正确转发
  4. 代理头配置:X-Forwarded-Proto用于告诉后端当前请求的协议(HTTPS),适配ALB的转发场景

内容的提问来源于stack exchange,提问作者usert4jju7

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 09:25:02