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

能否用Docker化Nginx搭建本地反向代理实现NextJS多域名访问?

用Docker容器部署Nginx反向代理映射本地域名到NextJS项目

我有两个NextJS项目,分别运行在localhost:3000和localhost:4000,希望通过独立Docker容器部署Nginx作为反向代理,将自定义本地域名mydomain.local映射到localhost:3000,auth.mydomain.local映射到localhost:4000。以下是当前配置文件及修正方案:

当前配置文件

nginx.conf

worker_processes 1;

events { 
    worker_connections 1024; 
}

http {

    server {
        listen 80;
        server_name mydomain.local;

        location / {
            proxy_pass http://localhost:3000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    server {
        listen 80;
        server_name auth.mydomain.local;

        location / {
            proxy_pass http://localhost:4000;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

}

docker-compose.yml

version: '3.7'

services:
  nginx:
    container_name: reverse_proxy
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - app_network

networks:
  app_network:
    external: true

Dockerfile

FROM nginx

# Remove the default Nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf

COPY nginx.conf /etc/nginx/conf.d/

# Expose port 80 for incoming connections
EXPOSE 80

配置问题分析

  • 容器内localhost指向错误:Nginx容器中的localhost指向容器自身,而非宿主机的localhost,无法访问宿主机上的NextJS项目。
  • Nginx配置结构冲突:默认Nginx主配置文件/etc/nginx/nginx.conf已包含http块,并通过include /etc/nginx/conf.d/*.conf加载子配置。当前nginx.conf包含完整的http和events块,复制到conf.d目录会导致配置重复,Nginx启动失败。
  • 外部网络无意义:若NextJS项目运行在宿主机而非Docker容器中,配置外部网络app_network无法帮助Nginx访问宿主机服务。

修改后的配置方案

1. 调整Nginx配置文件

将nginx.conf改为仅包含server块(作为子配置加载):

server {
    listen 80;
    server_name mydomain.local;

    location / {
        # Mac/Windows用host.docker.internal,Linux用宿主机IP或172.17.0.1(默认Docker网关)
        proxy_pass http://host.docker.internal:3000;
        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;
    }
}

server {
    listen 80;
    server_name auth.mydomain.local;

    location / {
        proxy_pass http://host.docker.internal:4000;
        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;
    }
}

2. 修改Dockerfile

保持原逻辑,仅复制调整后的子配置:

FROM nginx

RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/
EXPOSE 80

3. 简化docker-compose.yml

无需外部网络,直接映射端口和配置文件:

version: '3.7'

services:
  nginx:
    container_name: reverse_proxy
    build: .
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/nginx.conf:ro

4. 配置本地hosts文件

在系统hosts文件中添加映射:

127.0.0.1   mydomain.local
127.0.0.1   auth.mydomain.local

启动验证

  1. 确保两个NextJS项目正常运行在3000和4000端口
  2. 执行docker-compose up --build启动Nginx容器
  3. 访问http://mydomain.local和http://auth.mydomain.local验证反向代理是否生效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 13:48:11