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

如何用Nginx实现不同前缀路径代理至不同端口主机?

Solution for Nginx Single Domain Path-Based Proxying with Preserved Path Prefix

Absolutely, this setup is totally feasible—you just need to tweak your Nginx proxy configuration to fix that path prefix disappearing issue. Let’s break down what’s happening and how to fix it.

Why Your Original Config Causes the Path Prefix to Disappear

When you use the config:

location /path1 { proxy_pass http://10.0.0.1:8081; }

Nginx behaves like this: it takes the incoming request URI (e.g., /path1/some/url), strips off the matched /path1 prefix, and sends the remaining /some/url to the backend at 10.0.0.1:8081.

If your backend service responds with a redirect (say, to /some/url), the browser will receive that redirect and update the address bar to https://example.com/some/url—dropping the /path1 prefix entirely. That’s exactly what you’re seeing.

Fixes to Preserve the Path Prefix

There are two common approaches to resolve this, depending on your backend’s capabilities:

Option 1: Adjust Proxy Redirects to Rewrite Backend Responses

If your backend doesn’t support a base path (context path) and expects requests without the /path1 prefix, use proxy_redirect to rewrite any redirect URLs the backend sends back, adding the prefix back in. Here’s the updated config:

server {
    listen 443 ssl;
    server_name example.com;

    # Replace with your actual SSL certificate paths
    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    location /path1 {
        proxy_pass http://10.0.0.1:8081;
        # Rewrite backend redirects to add the /path1 prefix
        proxy_redirect http://10.0.0.1:8081/ /path1/;
        
        # Optional but recommended: Pass critical request headers to the backend
        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;
    }

    location /path2 {
        proxy_pass http://10.0.0.1:8082;
        proxy_redirect http://10.0.0.1:8082/ /path2/;
        
        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;
    }

    location /path3 {
        proxy_pass http://10.0.0.1:8083;
        proxy_redirect http://10.0.0.1:8083/ /path3/;
        
        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;
    }
}

The proxy_redirect directive tells Nginx to modify any Location response headers from the backend. For example, if the backend sends a redirect to /some/url, Nginx will rewrite it to /path1/some/url before sending it to the browser—keeping the prefix in the address bar.

Option 2: Pass the Full Request URI to the Backend

If your backend can handle requests with the /path1 prefix (e.g., you can configure it to use /path1 as its base context path), you can send the entire request URI to the backend instead of stripping the prefix. Use this config for each location:

location /path1 {
    proxy_pass http://10.0.0.1:8081$request_uri;
    
    # Keep the same proxy_set_header directives 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;
    proxy_set_header X-Forwarded-Proto $scheme;
}

This way, the full URI (like /path1/some/url) is sent directly to the backend. If your backend is configured to serve content under /path1, it’ll respond correctly, and any links in its responses will already include the prefix—no redirect rewriting needed.

Bonus: Configure Backend Context Path (If Possible)

If you control the backend service, the cleanest solution is to set its base context path to match the Nginx location prefix. For example:

  • For a Spring Boot app, add server.servlet.context-path=/path1 to application.properties
  • For a Node.js/Express app, use app.use('/path1', yourRouter)

This way, your original simple proxy_pass config will work perfectly, as the backend expects the prefix and will generate links that include it automatically.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:49:40