【问题标题】:node.js app with nginx 502 bad gateway error带有 nginx 502 错误网关错误的 node.js 应用程序
【发布时间】:2016-01-22 10:38:21
【问题描述】:

我正在使用 nginx 配置我的 node.js 应用程序。它适用于 http,但不适用于 https。当我尝试访问安全域时。我得到这个错误。

502 Bad Gateway
nginx/1.4.6 (Ubuntu)

这是我的 nginx 配置文件

    upstream node_app_dev {
        server 127.0.0.1:3000;
    }

    upstream node_app_production {
        server 127.0.0.1:3000;
    }

server {
    listen 80;
    server_name mydomain.com;
    access_log /var/log/nginx/dev.log;
    error_log /var/log/nginx/dev.error.log debug;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://node_app_dev;
        proxy_redirect off;
    }
}


server {
    listen 443 ssl;
    server_name mydomain.com;
    access_log /var/log/nginx/secure.log;
    error_log /var/log/nginx/secure.error.log debug;    

    ssl on;
    ssl_certificate certs/mycert.crt;
    ssl_certificate_key certs/mykey.key;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;        
        proxy_pass https://node_app_production;
        proxy_redirect off;

    }    
}

【问题讨论】:

  • stackoverflow.com/questions/10375659/nginx-proxy-pass-node-ssl 的可能重复项:如果您使用 nginx 处理 SSL,那么您的节点服务器将只使用 http。
  • 我的 node.js 服务器是简单的 http 服务器。我正在使用 nginx 处理 https,它指向简单的 http 服务器。
  • 我很高兴它对你有用。你愿意投票给我的答案吗?

标签: node.js nginx


【解决方案1】:

替换

proxy_pass https://node_app_production;

proxy_pass http://node_app_production;

重启 nginx,你应该已经准备好了。 见nginx proxy pass Node, SSL?

【讨论】:

  • 从以前的文件中复制粘贴的位置设置,而不注意 https。 :)
【解决方案2】:

我已经通过 2 个步骤解决了这个问题。

  1. 检查/var/log/nginx/error.log
connect() failed (111: Connection refused) while connecting to upstream, client: *.*.*.*, server: *.*.*.*, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "*.*.*.*"

       即使我在 nginx conf 文件中将上游设置为 127.0.0.1:3000,上游仍然是 127.0.0.1:8000

  1. /etc/nginx/conf.d/virtual.conf中的server 127.0.0.1:8000替换为server 127.0.0.1:3000并重启nginx。
server {
    listen       80;
    server_name  SERVER_IP_ADDRESS;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}
sudo /etc/init.d/nginx restart 

最后,它没有 502 错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-04
    • 2011-05-14
    • 2019-06-05
    • 2015-08-10
    • 1970-01-01
    • 2020-06-29
    • 1970-01-01
    • 2020-02-13
    相关资源
    最近更新 更多