【问题标题】:port open but getting connection refused ...centos 8 / nginx / react / nodejs端口打开但连接被拒绝...centos 8 / nginx / react / nodejs
【发布时间】:2021-04-04 20:57:33
【问题描述】:

我在 pm2 上运行 nodejs 并在端口 5000 上侦听。如果我在服务器上运行netstat -na |grep 5000,我会得到

tcp 0 0 0.0.0.0:5000 0.0.0.0:* LISTEN

但是当我的 react 应用程序调用 http://localhost:5000/patients 我得到

::ERR_CONNECTION_REFUSED

这是服务器的代码:

const app = express();
const port = process.env.PORT || 5000;

app.use('/patients', require("./routes/patientRoute"));


app.listen(port, "0.0.0.0", () => {
    console.log(`server is running on port: ${port}`);
})

从 React 生产构建中,我使用 axios 运行 GET 到 http://localhost:5000/patients 这就是错误发生时

我也在firewalld上打开了端口

【问题讨论】:

  • 这可能是一个 selinux 问题。
  • 我跑了 setenforce 0 ...它仍然什么也没做
  • 我也有同样的问题。你有机会解决它吗?
  • @MarcoGazzin 是的,首先我必须摆脱“0.0.0.0”,然后我将 /api/ 添加到我所有 axios 调用的开头,然后在我需要的 nginx conf 文件中添加location /api/ { proxy_pass http://127.0.0.1:5000; }.....NGINX 中的位置块是关键!

标签: node.js reactjs axios centos connection-refused


【解决方案1】:

首先我必须摆脱“0.0.0.0”,然后我在所有 axios 调用中将 http://localhost:5000 替换为 /api/,然后在我的 nginx conf 文件中我需要添加位置 /api/ { proxy_pass http://127.0.0.1:5000; } .....NGINX 中的 location 块是关键!

【讨论】:

    猜你喜欢
    • 2015-06-14
    • 1970-01-01
    • 2016-05-27
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    相关资源
    最近更新 更多