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

请求Nginx配置帮助:带路径追加的域名转发与URL重写

Fixing Nginx Proxy Path Rewrite for Cross-Domain Forwarding

Got it, let's get this proxy issue sorted out for you. The core problem here is that you need to rewrite the incoming request path from /downloads to /app/downloads while preserving query parameters, all while keeping the browser showing service1.com URLs.

Why Your Previous Config Failed

When you used proxy_pass $upstream_endpoint/app/$args;, a couple of key issues broke the expected behavior:

  • Using a variable in proxy_pass changes how Nginx handles URI rewriting—it won’t automatically replace the matched /downloads path with your appended /app/ segment
  • Adding $args directly to the proxy_pass string mangles the path structure, leading to upstream URLs like /app/?param1=a instead of the intended /app/downloads?param1=a

Solution 1: Use rewrite for Variable Upstreams

If you need to keep using the $upstream_endpoint variable (for dynamic upstream targets), use a rewrite rule to explicitly adjust the request path before proxying:

location /downloads {
    set $upstream_endpoint https://service2.com;
    
    # Rewrite path: /downloads → /app/downloads, preserve any trailing segments
    rewrite ^/downloads(.*)$ /app/downloads$1 break;
    
    proxy_pass $upstream_endpoint;
    
    # Optional but recommended: Preserve original request headers for upstream
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
  • The rewrite rule captures everything after /downloads (including an empty string) and appends it to /app/downloads
  • break stops further rewrite processing after this rule
  • Query parameters are automatically preserved by Nginx—no need to manually add $args

Solution 2: Direct Path Mapping (Simpler for Fixed Upstreams)

If your upstream address is static, you can skip the variable and let Nginx handle path mapping automatically:

location /downloads {
    proxy_pass https://service2.com/app/downloads;
    
    # Same header preservation as above
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
  • Here, Nginx replaces the matched /downloads path with /app/downloads in the upstream request
  • Query parameters stay intact automatically, so https://service1.com/downloads?param1=a&param2=b will correctly forward to https://service2.com/app/downloads?param1=a&param2=b

Testing the Config

Always validate your Nginx config before restarting to avoid downtime:

nginx -t

If the test passes, restart Nginx to apply changes:

# For systemd-based systems (Ubuntu, RHEL 8+, etc.)
systemctl restart nginx

# For SysVinit systems
service nginx restart

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:42:45