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

Docker部署生产环境MERN应用仅显示NGINX欢迎页问题排查

生产环境Docker化MERN应用访问仅显示NGINX默认页问题排查

我是Docker和NGINX新手,正在实践将MERN应用Docker化部署到生产环境(计划部署在AWS)。开发环境配置已完成,但生产环境访问时仅显示“Welcome to NGINX!”页面。

相关配置文件

前端Dockerfile.prod

FROM node:18-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build


FROM nginx
# Copy build assets from build/ folder & place them in nginx/html
COPY --from=build /app/build /usr/share/nginx/html
EXPOSE 80

生产环境NGINX配置文件prod.conf

upstream client { 
    server client:3000;
}
upstream api {
    server api:3001;
}

server {
    listen 80;
    
    location / { 
        root /usr/share/nginx/html;
        include /etc/nginx/mime.types;
        try_files $uri $uri/ /index.html;
    }
    
    location /sockjs-node {
        proxy_pass http://client;
        proxy_http_version 1.1;
        # setting 'Upgrade' allows a tunnel setup between client & proxied server
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    location /api {
        proxy_pass http://api;
    }
}

NGINX的Dockerfile.prod

FROM nginx
COPY ./prod.conf /etc/nginx/conf.d/default.conf

docker-compose.prod.yml

version: '3'
services:
  nginx:
    depends_on:
      - api
      - client
    restart: always
    build:
      dockerfile: Dockerfile.prod
      context: ./nginx
    ports:
      - "3000:80"
  api:
    build:
      dockerfile: Dockerfile
      context: ./backend
    volumes:
      - '/app/node_modules'
      - './backend:/app'
    command: npm start
  client:
    build: 
      dockerfile: Dockerfile.prod
      context: ./frontend
    volumes:
      - '/app/node_modules'
      - './frontend:/app'
    environment:
      - WDS_SOCKET_PORT=0
    tty: true
    stdin_open: true

目录结构

.
├── frontend
│   ├── Dockerfile
│   ├── Dockerfile.prod
│   ├── babel.config.json
│   ├── nginx
│   │   └── default.conf
├── nginx
│   ├── Dockerfile
│   ├── Dockerfile.prod
│   ├── default.conf
│   └── prod.conf
├── docker-compose.prod.yml
├── docker-compose.yml

问题分析

我原本预期的流程是:先通过NGINX的Dockerfile.prod构建NGINX容器并替换default.conf;再构建client容器,将前端构建产物复制到NGINX的/usr/share/nginx/html目录覆盖默认欢迎页。但实际查看该目录时,发现构建产物并未覆盖,且从Docker Compose日志看,client容器比NGINX容器先构建。我怀疑是NGINX的Dockerfile.prod覆盖了前端Dockerfile.prod中对NGINX的操作,请求帮忙排查问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 18:38:24