我在AWS EC2實例上爲我的node.js應用程序運行nginx。我想使用websockets(socket.io)和普通的http請求/響應。我的問題是,每當我從移動設備到服務器有一個活動套接字連接並嘗試發出一個普通的http請求時,就會通過消息「502 Bad Gateway」調用移動設備的socket.io錯誤函數。Socket.io斷開http請求
只有插座有效。只有普通的http請求也適用。
我想通了,這個問題發生在我將nginx設置爲僅使用https之後。
這裏是我的nginx的配置在/啓用站點-/站點可用:
server {
listen 80;
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
if ($scheme != "https") {
rewrite^https://$host$request_uri? permanent;
}
location/{
proxy_pass http://127.0.0.1:3000;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
}
}
Nginx的錯誤日誌:
[error] 14117#14117: *50 upstream prematurely closed connection while reading response header from upstream, client: 78.94.9.226, server: example.com, request: "GET /socket.io/?transport=polling&b64=1&sid=rgXMQhL6mbSET8ktAAAA HTTP/1.1", upstream: "http://127.0.0.1:3000/socket.io/?transport=polling&b64=1&sid=rgXMQhL6mbSET8ktAAAA", host: "example.com"
iOS的錯誤日誌:
LOG SocketIOClient: Handling event: error with data: ["Got unknown error from server <html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor=\"white\">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.10.3 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n"]
如果您需要更多的信息,讓我知道!
感謝您分享解決方案。我想PM2會帶參數來觀察.js,.css等的變化。如果是這樣,你應該使用 –
我注意到PM2自己記錄http請求+ stdout/stderr,所以我禁用摩根寫入文件。 PM2日誌足以滿足我的需求。 –