【发布时间】:2021-03-29 10:44:07
【问题描述】:
我正在使用 Nginx 配置(来自 Nginx sites-available 文件夹)为 React 应用程序和 NodeJS API 提供服务:
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name my-site-name.com www.my-site-name.com;
location /api/ {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
}
location / {
root /var/www/html/;
try_files $uri /index.html;
}
}
如果我在服务器上使用npm start 启动 NodeJS API,一切正常。如果我使用 PM2 将 API 作为进程运行 - 我会通过调用任何 API 端点得到 502 Bad gateway。
编辑:PM2 不在预期端口 (3000) 提供应用程序。关掉就知道了。
【问题讨论】:
-
在
npm run build之后,您的文件是否位于var/www/html或var/www/html/your_build_folder中? -
我在两种情况下都尝试了两种方式 - React 应用程序的静态部分正在工作(当然,在 nginx 配置中进行了额外的更改)。只有 API 部分不起作用
标签: node.js reactjs nginx http-status-code-502