[英]Nginx PM2 NodeJS reverse-proxy with SSL gives HTTP 504
曾經有一段美好的時光,我對三個NodeJ進行了很好的配置, socket.io API運行在同一台Ubuntu 16.4 LTS VPS服務器上,其中PM2用於進程管理,而Nginx用於反向代理到三個不同的子域。
我已經成功安裝了來自Let's Encrypt的SSL證書,並且所有子域都來自同一個域(例如exemple.com),應重定向到https 。
一旦我嘗試為非NodeJs應用程序(PHP / laravel)添加第四個子域,就不再傳遞反向代理,而且很遺憾,我沒有舊Nginx配置的備份。
現在,我試圖恢復我的VPS與三個舊的NodeJs應用的協調,但是它使Nginx產生了504網關超時 。
這是我認為與舊配置相同的配置:
此配置在chrome上可以正常使用,但我正嘗試從移動和桌面應用程序訪問我的API。
# HTTP — redirect all traffic to HTTPS
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
# App1 from port 3000 to sub1.exemple.com
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub1.exemple.com;
# Use the Let’s Encrypt certificates
ssl_certificate
/etc/letsencrypt/live/sub1.exemple.com/fullchain.pem;
ssl_certificate_key
/etc/letsencrypt/live/sub1.exemple.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location / {
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;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# App2 from port 4000 to sub2.exemple.com
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub2.exemple.com;
# Use the Let’s Encrypt certificates
ssl_certificate
/etc/letsencrypt/live/sub2.exemple.com/fullchain.pem;
ssl_certificate_key
/etc/letsencrypt/live/sub2.exemple.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location / {
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;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://localhost:4000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
# App2 from port 5000 to sub3.exemple.com
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name sub3.exemple.com;
# Use the Let’s Encrypt certificates
ssl_certificate
/etc/letsencrypt/live/sub3.exemple.com/fullchain.pem;
ssl_certificate_key
/etc/letsencrypt/live/sub3.exemple.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location / {
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;
proxy_set_header X-NginX-Proxy true;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_pass http://localhost:5000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Nginx,NodeJ和PM2沒有任何錯誤。 日志是干凈的。 這是我在檢查請求時得到的。
當套接字請求時成功:( wss://
和https://
)
當其他人請求時失敗:
我還想指出的是,每個子服務器都安裝了SSL,並且應用程序穩定且可以在本地服務器上運行,沒有任何問題。
我發現了問題,它不是不是在Nginx中 , 不是在PM2中 ,也不在Nodejs中, 不在 SSL中 ,而是在我部署的所有應用程序中。 Mongodb的過程中出現問題,使他無法自動啟動 。 因此,該應用程序接受拳頭請求是因為它不需要數據庫干預,並且在超時后拒絕了登錄請求,因為該應用程序已經崩潰,但是PM2重新啟動它,並且Nginx保持子域對請求保持打開狀態。
FF:因此,如果您在此處經過,則可能需要檢查您的應用程序環境。 例如:SGBD,R / W權限,API ...
希望這可以幫助任何有類似問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.