【问题标题】:nodejs nginx 502 gateway errornodejs nginx 502网关错误
【发布时间】:2018-01-05 04:37:53
【问题描述】:

我正在尝试使用 nginx 反向代理后面的 nodejs 应用程序来处理 ssl

我的应用程序在 localhost:2000 上运行。我可以确认这是使用 curl 命令。

这是我的 nginx 设置:

  # the IP(s) on which your node server is running. I chose port 3000.
upstream dreamingoftech.uk {
server 127.0.0.1:2000;
keepalive 16;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name dreamingoftech.uk;
return 301 https://$host$request_uri;
}
#HTTPS
server {
listen 443 ssl http2;

server_name dreamingoftech.uk;
access_log /var/log/nginx/dreamingoftech.log;
error_log /var/log/nginx/dreamingoftech.error.log debug;

ssl_certificate /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/dreamingoftech.uk/privkey.pem;

include snippets/ssl-params.conf;

# pass the request to the node.js server with the correct headers and much more can be added, see nginx config options
location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-Forwarded-Proto https;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass http://dreamingoftech.uk/;
  proxy_redirect off;
  #proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "";
  proxy_ssl_session_reuse off;
  proxy_cache_bypass $http_upgrade;
}
}

如果我现在 curl https://dreamingoftech.uk,这需要一段时间,但我确实收到了网页。尽管带有消息:

curl: (18) 传输关闭,剩余 1 个字节要读取

但是,当从浏览器查看时,我收到 502 网关错误。

我检查了错误日志,结果如下:ERROR LOG

我不明白为什么反向代理会在流程中添加这样的时间延迟。任何想法将不胜感激。

PS:在上游配置中,我尝试使用 localhost 而不是 127.0.0.1 无济于事

【问题讨论】:

  • 你确定你的应用程序在代理后面没有不同的行为,比如基于远程 IP 的行为不同吗?
  • @BrahmaDev 老实说,我不知道......我现在正在努力学习所有这些,所以我不知道如何测试?

标签: node.js nginx nginx-reverse-proxy


【解决方案1】:

我的配置几乎相同。你可以试试下面的

您可以将所有http重定向到https

server {
    listen  80;
    return 301 https://$host$request_uri;
}

或者对于这样的特定网站

server {
    server_name dreamingoftech.uk;
    return 301 https://dreamingoftech.uk$request_uri;
}

但只为你的情况选择一个

然后确保您的节点服务器在 http 模式下运行并且不是 https。

您还提到您在端口 3000 上运行节点,然后使用端口 3000 而不是我在您的配置中看到的 2000。

在您确认以上将所有数据包重定向到本地主机后,像这样

server {

    listen 443;
    server_name dreamingoftech.uk;

    ssl_certificate           /etc/letsencrypt/live/dreamingoftech.uk/fullchain.pem;
    ssl_certificate_key       /etc/letsencrypt/live/dreamingoftech.uk/privkey.pem;

    ssl on;
    ssl_session_cache  builtin:1000  shared:SSL:10m;
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;

    location / {

      proxy_set_header        Host $host;
      proxy_set_header        X-Real-IP $remote_addr;
      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header        X-Forwarded-Proto $scheme;

      # Fix the “It appears that your reverse proxy set up is broken" error.
      proxy_pass          http://localhost:3000;
      proxy_read_timeout  90s;

      proxy_redirect      http://localhost:3000 https://dreamingoftech.uk;
    }
 }

创建一个文件并将上面的代码放在sites-available 中,名称类似于dreamingoftech.uk,然后使用ln -s 创建指向sites-enabled 的软链接。转到您的 nginx.conf 并确保包含文件夹 sites-enabled

然后必须重启 nginx 来检查它是否工作

【讨论】:

    【解决方案2】:

    @Stamos 感谢您的回复。我试过了,但不幸的是它没有用。我决定尝试最基本的节点应用程序,我仍然可以使用我正在使用的基本模块。

    我试过了,效果立竿见影。

    因此,问题出在我的应用程序上。我会花时间逐步重建和测试,直到找到问题为止,

    感谢您的宝贵时间!

    【讨论】:

      猜你喜欢
      • 2011-05-14
      • 2019-06-05
      • 2015-08-10
      • 2021-11-19
      • 2012-09-25
      • 2014-12-07
      • 2020-09-29
      • 2012-07-16
      • 2015-04-30
      相关资源
      最近更新 更多