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

配置本地Nginx代理Docker容器端口并复现502 Bad Gateway错误的技术求助

配置本地Nginx代理Docker容器端口并复现502 Bad Gateway错误的技术求助

各位大佬好,这可能是个新手问题,但我折腾了好一会儿还是没搞明白,想请教下大家怎么操作🙏

我目前的情况是这样的:

  • 本地跑了个Ubuntu 20.04.6 LTS的虚拟机,刚用命令sudo apt-get install nginx装了Nginx,版本是nginx/1.18.0 (Ubuntu)(用nginx -v确认的),而且通过systemctl status nginx也确认服务是正常运行的。

  • 然后我写了个简单的Dockerfile和Node.js的Express服务,做成了Docker容器:
    Dockerfile内容:

    # Use the official Node.js image with Alpine Linux as the base image
    FROM node:14-alpine
    
    # Set the working directory in the container
    WORKDIR /usr/src/app
    
    # Copy package.json and package-lock.json to the working directory
    COPY package*.json ./
    
    # Install dependencies
    RUN npm install
    
    # Copy the rest of the application code to the working directory
    COPY . .
    
    # Expose the port that the app will run on
    EXPOSE 9000
    
    # Define the command to run your application
    CMD ["node", "app.js"]
    

    对应的app.js:

    // app.js
    const express = require('express');
    const app = express();
    const port = 9000;
    
    app.get('/', (req, res) => {
      res.send('<h1>Hello, Docker World!</h1>');
    });
    
    app.listen(port, () => {
      console.log(`App listening at http://localhost:${port}`);
    });
    

    启动容器后,直接访问localhost:9000能正常显示“Hello, Docker World!”,没问题。

现在我的需求是:
想配置Nginx,让它把请求转发到这个Docker容器的9000端口,这样我访问Nginx的时候就能看到Node服务的内容;之后我关掉Docker容器,就能触发Nginx返回502 Bad Gateway错误,以此来复现这个场景。

但我不知道该怎么配置Nginx的代理规则,有没有大佬能教教我具体的步骤呀?

备注:内容来源于stack exchange,提问作者Curious Developer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.21 08:35:28