nodejs nginx 502网关错误

问题描述 投票:0回答:2

我试图在nginx反向代理后面使用nodejs app来处理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;
}
}

如果我现在卷曲https://dreamingoftech.uk,它需要一段时间,但我确实得到了网页。虽然有这样的信息:

卷曲:(18)转移关闭,剩余1个字节读取

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

我检查了错误日志,这是结果:ERROR LOG

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

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

node.js nginx nginx-reverse-proxy
2个回答
0
投票

我有几乎相同的配置。你能试试以下吗?

您可以将所有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,正如我在你的配置中看到的那样。

确认以上将所有数据包重定向到localhost之后

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以检查它是否有效


0
投票

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

我尝试了这个,它立即起作用。

问题出在我的应用程序上。我会一步一步地重建和测试,直到找到问题为止,

谢谢你的时间!

© www.soinside.com 2019 - 2024. All rights reserved.