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

基于Nginx实现Docker代理路由:域名路径访问多容器应用

Hey there! Let's get your path-based routing set up with Nginx reverse proxy for your Docker Compose apps. Here's a step-by-step solution tailored to your scenario:

Step 1: Create the Nginx Configuration File

First, make a nginx.conf file with the reverse proxy rules. This config will route requests to example.com/app1 to your app1's 1111 port, and example.com/app2 to app2's 3333 port.

server {
    listen 80;
    server_name example.com;

    # Route for app1
    location /app1/ {
        # The trailing slash here is CRUCIAL — it strips /app1/ from the forwarded path
        proxy_pass http://localhost:1111/;
        
        # Pass necessary headers to the backend app
        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;
    }

    # Route for app2
    location /app2/ {
        proxy_pass http://localhost:3333/;
        
        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;
    }

    # Optional: Handle root path (redirect to app1 or show a landing page)
    location / {
        return 302 /app1/;
        # Uncomment below to show Nginx's default page instead
        # root /usr/share/nginx/html;
        # index index.html;
    }
}

Step 2: Set Up Nginx with Docker Compose

Create a docker-compose.yml for the Nginx proxy to run it as a container, mounting your custom config:

version: '3.8'

services:
  nginx-proxy:
    image: nginx:alpine
    ports:
      - "80:80"
      # Uncomment below if you need HTTPS (don't forget to add SSL certs)
      # - "443:443"
    volumes:
      # Mount your custom config into the container
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
      # If using HTTPS, mount your SSL certificate and key here
      # - ./ssl/cert.pem:/etc/nginx/ssl/cert.pem
      # - ./ssl/key.pem:/etc/nginx/ssl/key.pem
    restart: always

    # Optional: If your apps use custom Docker networks, add Nginx to them here
    # networks:
    #   - app1-network
    #   - app2-network

# Declare external networks if you added them above (they must exist from your app's compose files)
# networks:
#   app1-network:
#     external: true
#   app2-network:
#     external: true

Key Things to Keep in Mind

  • Trailing Slashes Matter: The / at the end of proxy_pass http://localhost:1111/ ensures that requests like example.com/app1/login get forwarded to http://localhost:1111/login, not http://localhost:1111/app1/login (which would break your app).
  • Network Access: If your apps don't expose ports to the host machine (using expose instead of ports in their compose files), it's better to connect Nginx to your apps' custom Docker networks. Then you can use the app service name instead of localhost, e.g., proxy_pass http://app1-web:1111/ (replace app1-web with your actual service name from app1's compose).
  • App Path Compatibility: Some apps hardcode absolute paths (like /css/style.css). In this case, you'll need to either:
    1. Configure your app to run under a base path (e.g., /app1 — check your app's docs for base URL settings), or
    2. Add sub_filter rules to Nginx to rewrite paths in the response:
      sub_filter '/css/' '/app1/css/';
      sub_filter '/js/' '/app1/js/';
      sub_filter_once off;
      
  • HTTPS Setup: To add HTTPS, create a ssl folder with your certificate and key, then add a server block in nginx.conf listening on 443 with SSL settings, and redirect 80 traffic to 443.

Once you've set this up, start the Nginx proxy with docker-compose up -d, and you should be able to access your apps via example.com/app1 and example.com/app2!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:43:29