Nginx反向代理到NodeJS App导致502错误网关错误

时间:2019-01-02 12:37:12

标签: node.js reactjs express nginx

我正在设置nginx,以便可以通过example.com/api

这样的URL访问使用express构建的API。

这是我的Nginx配置

upstream appfrontend {
    server localhost:9008 fail_timeout=0;
}

upstream api {
server localhost:3001;
}

server {
    listen 80;
    listen [::]:80;

    server_name hospoline.com www.hospoline.com;
    return 301 https://$server_name$request_uri;
}

server {
    listen 443 ssl;
    listen [::]:443;

    server_name example.com; # replace this with your domain

    root /var/www/html/example-certbot-webroot;

    # The public and private parts of the certificate are linked here
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location /.well-known {
        root /var/www/html/example;
    }
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://appfrontend;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 900s;
    }
    location /api {
    proxy_pass http://api;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

}

当我访问我的网站example.com时,前端会完美加载。

当我访问 example.com/api/fetch_doctors时,我收到502错误的网关错误。

我的API在本地主机上运行良好。当我向本地计算机上的localhost:3001发送请求时,我得到了医生列表。

我的前端服务器和后端服务器都永远使用。

我真的迷失了自己,整整一整天都为此感到沮丧。我不知道我要去哪里错了。在正确方向上的任何帮助都将非常有用!谢谢大家!

0 个答案:

没有答案