簡體   English   中英

nodejs nginx 502網關錯誤

[英]nodejs nginx 502 gateway error

我正在嘗試在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;
}
}

如果我現在卷曲https://dreamingoftech.uk ,則需要花費一些時間,但是我確實獲得了交付的網頁。 盡管顯示以下消息:

curl:(18)傳輸關閉,剩余1個字節可讀取

但是,從瀏覽器查看時,出現502網關錯誤。

我已經檢查了錯誤日志,這是結果: 錯誤日志

我不明白為什么反向代理會在流程中增加這樣的時間延遲。 任何想法將不勝感激。

PS:在上游配置中,我嘗試了localhost而不是127.0.0.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提供名稱的sites-available例如dreamingoftech.uk並使用ln -s創建到sites-enabled的軟鏈接。 轉到您的nginx.conf並確保您包括已sites-enabled文件夾的sites-enabled

然后必須重新啟動 nginx以檢查它是否有效

@Stamos感謝您的回復。 我嘗試過,但是不幸的是它沒有用。 我決定嘗試仍可以使用我正在使用的基本模塊的最基本的節點應用程序。

我嘗試了一下,它立即起作用。

問題出在我的應用程序上。 我將花一些時間逐步重建和測試,直到發現問題,

謝謝你的時間!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM