【发布时间】:2021-12-03 09:59:25
【问题描述】:
我在 Amazon Web 服务器中运行了一个连接到 Express APIREST 的 ReactJS 前端。使用 PM2 启动进程和 NGINX。
当我尝试在 chrome 中登录应用程序时出现问题,前端没问题,但它拒绝连接到 APIREST。 Error Image
这些是我拥有的文件:
APIREST .env
DB_CCT=my_db
DB_CCT_USER=user
DB_CCT_PASS=password
DB_CCT_HOST=127.0.0.1
NODE_ENV=development
PORT=8017
JWT_SECRET=jotauvedoblete
前端 ReactJS .env
我也尝试添加公共 IP 而不是 localhost 或 127.0.0.1 但也不起作用,它显示连接超时。
REACT_APP_DB=http://localhost:8017/api/v1/
PM2 生态系统.config.js
module.exports = {
apps: [{
name: 'CCT_REACT',
cwd: 'cct-web-client/',
script: 'npm',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'run start:production',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 5001
},
env_production: {
NODE_ENV: 'production',
PORT: 5001
}
},
{
name: 'CCT_API_REST',
cwd: 'cct_api_rest/',
script: 'npm',
// Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
args: 'run start',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'production',
PORT: 8017
},
env_production: {
NODE_ENV: 'production',
PORT: 8017
}
}],
deploy: {
production: {
}
}
}
NGINX 服务
server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/CCT/cct-web-client/build;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri/ /index.html?q=$uri&$args;
}
}
【问题讨论】:
-
由于前端在浏览器中本地运行并向 AWS 服务器上的远程 API 发送请求,因此必须将请求发送到 remote 服务器的地址。
localhost指向运行前端的 local 机器,因此它绝对不是 API 的正确路径。 -
当您尝试访问公共服务器的 80 端口 IP 时是否收到任何响应?如果配置正确,你应该会得到 nginx 的默认登陆页面。这将说明至少 nginx 配置是否正确以及服务器实际上能够与外部通信。
-
你是对的,但如果不是 localhost 我添加公共 ip 错误是 net::ERR_CONNECTION_TIMED_OUT 是的,在端口 80 上我正确地得到了前端
-
这应该是需要关注的问题,是的。不幸的是,使用 localhost 是无关紧要的。通过
curling AWS 服务器外壳内的 localhost 确保 Express 实际运行。另外,检查pm2 logs是否有错误。
标签: javascript reactjs express nginx pm2