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

如何通过location配置单端口部署多Java Web应用及Nginx重定向问题

Hey there, let's break down your Nginx problems and fix them step by step!

1. Fixing the Redirect & Static Resource Path Issue

The core problem here is that your backend Java app doesn't know it's being served under the /lc context path. So when it generates redirect URLs or references JS/CSS files, it uses root paths (/) instead of including the /lc prefix. Here's how to adjust your Nginx config to fix this:

Update your server block with these additions:

server {
    listen 8082;

    location /lc/ {
        proxy_pass http://127.0.0.1:8080/;  # Notice the trailing / here—it maps /lc/xxx to backend /xxx
        proxy_set_header Host $http_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;

        # Fix redirect URLs sent by the backend—rewrite root paths to include /lc/
        proxy_redirect http://127.0.0.1:8080/ /lc/;
        proxy_redirect / /lc/;  # Catch-all for relative redirects

        # Rewrite absolute resource paths in HTML (JS/CSS links) to include /lc/
        sub_filter 'href="/' 'href="/lc/';
        sub_filter 'src="/' 'src="/lc/';
        sub_filter_once off;  # Replace all matches, not just the first one
    }
}

Key notes about this config:

  • Trailing / in proxy_pass: This tells Nginx to strip the /lc/ prefix when forwarding requests to the backend, so the app sees paths like /login instead of /lc/login.
  • proxy_redirect: This modifies the Location header in backend responses, turning http://127.0.0.1:8080/login into http://127.0.0.1:8082/lc/login—fixing the missing prefix on redirects.
  • sub_filter: This scans the HTML content and replaces root-relative resource links (like /js/main.js) with /lc/js/main.js, ensuring your static assets load correctly. Note: Make sure your Nginx was compiled with the http_sub_module—if not, you'll need to recompile Nginx or install a package that includes it.

A cleaner long-term fix: If you can modify your Java app's config, set its context path directly to /lc (e.g., in Spring Boot, add server.servlet.context-path=/lc to application.properties). This way, the app generates all URLs with the /lc prefix natively, and you can skip the sub_filter setup entirely.

2. Deploying Multiple Java Web Apps on a Single Port

To host multiple Java apps on one Nginx port, you just need to create separate location blocks for each app, each with a unique URL prefix pointing to its backend port. Here's an example for three apps:

server {
    listen 8082;

    # App 1: /lc/ prefix → backend port 8080
    location /lc/ {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header Host $http_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;

        proxy_redirect http://127.0.0.1:8080/ /lc/;
        proxy_redirect / /lc/;

        sub_filter 'href="/' 'href="/lc/';
        sub_filter 'src="/' 'src="/lc/';
        sub_filter_once off;
    }

    # App 2: /app2/ prefix → backend port 8081
    location /app2/ {
        proxy_pass http://127.0.0.1:8081/;
        proxy_set_header Host $http_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;

        proxy_redirect http://127.0.0.1:8081/ /app2/;
        proxy_redirect / /app2/;

        sub_filter 'href="/' 'href="/app2/';
        sub_filter 'src="/' 'src="/app2/';
        sub_filter_once off;
    }

    # App 3: /app3/ prefix → backend port 8082
    location /app3/ {
        proxy_pass http://127.0.0.1:8082/;
        proxy_set_header Host $http_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;

        proxy_redirect http://127.0.0.1:8082/ /app3/;
        proxy_redirect / /app3/;

        sub_filter 'href="/' 'href="/app3/';
        sub_filter 'src="/' 'src="/app3/';
        sub_filter_once off;
    }
}

Now you can access each app via:

  • http://127.0.0.1:8082/lc → App 1
  • http://127.0.0.1:8082/app2 → App 2
  • http://127.0.0.1:8082/app3 → App 3

Again, setting each app's native context path to match its Nginx prefix will simplify the config and avoid relying on sub_filter.

After making these changes, reload Nginx to apply them:

nginx -s reload

内容的提问来源于stack exchange,提问作者桃花幻梦

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:53:44