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

远程主机Express服务访问报502错误,是否受Nginx干扰?

问题:Express服务部署后访问出现502错误排查

我的Node.js Express服务在本地端口4000运行正常,但部署到公网IP为129.226.92.242的远程Linux主机后,访问http://129.226.92.242:4000或http://129.226.92.242:4000/hello时出现502错误。我已关闭防火墙,主机上运行有Nginx服务,以下是我的Express代码和Nginx配置,请问是否是Nginx干扰了Express静态服务的访问?

Express代码

// Import builtin NodeJS modules to instantiate the service
const http = require("http");
const port = 4000;

// Import the express module
const express = require("express");

// Instantiate an Express application
const app = express();

// app.get("/", (req, res) => {
//   res.send("Hello from express server.");
// });

// Create a NodeJS HTTPS listener on port 4000 that points to the Express app
// Use a callback function to tell when the server is created.
// http.createServer(app).listen(4000, () => {
//   console.log("server is runing at port 4000");
// });

// Create an try point route for the Express app listening on port 4000.
// This code tells the service to listed to any request coming to the / route.
// Once the request is received, it will display a message "Hello from express server."
app.use(express.static("dist"));
app.get("/hello", (req, res) => {
  res.send("Hello World!");
});
app.listen(port, () => {
  console.log(`Server listening on port:${port}`);
});

Nginx配置

user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

  }
}

排查与解决思路

1. 当前Nginx不会直接干扰4000端口访问

你的Nginx监听的是80端口,和Express的4000端口没有端口冲突,直接访问4000端口出现502,问题不在Nginx干扰上,需从以下方向排查:

2. 确认Express服务在远程主机是否正常运行

  • 登录远程主机,执行ps aux | grep node,查看是否有你的Node进程在运行。
  • 如果进程未启动,进入项目目录执行npm install安装依赖,再启动服务node 你的服务文件名.js。
  • 如果进程在运行,在主机本地执行curl http://localhost:4000/hello,看能否拿到"Hello World!"响应。如果本地curl失败,说明服务本身有问题(比如dist目录不存在、权限不足)。

3. 修正Express监听地址(关键)

你的代码中app.listen(port, () => { ... })未指定监听地址,默认仅监听localhost(只能本地访问),公网无法连接。
修改为:

app.listen(port, '0.0.0.0', () => {
  console.log(`Server listening on port:${port}`);
});

这样服务会监听主机所有网卡地址,公网才能访问到4000端口。

4. 验证端口是否对外开放

  • 即便关闭了主机防火墙,部分云服务商的安全组/网络ACL仍会限制端口,需在云平台控制台开启4000端口的入站规则。
  • 可通过本地执行telnet 129.226.92.242 4000,检测端口是否能连通。

5. 推荐:用Nginx反向代理(避免直接暴露4000端口)

如果不想直接暴露4000端口,可配置Nginx将80端口请求转发到4000端口,修改Nginx的server块:

server {
    listen       80;
    server_name  129.226.92.242; # 替换为你的公网IP或域名

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

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

修改后重启Nginx:nginx -s reload,之后访问http://129.226.92.242即可直接访问Express服务。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 21:30:24