我使用 Nginx 配置(来自 Nginxsites-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;
}
}
如果我在服务器上启动 NodeJS API npm start
- 一切正常。如果我使用 PM2 作为进程运行 API - 我通过调用任何 API 端点得到 502 Bad gateway。
编辑:PM2 不在预期端口(3000)提供应用程序。关掉就知道了。