简体   繁体   English

SSL上带有Sails.js / Node.js和NGINX的Socket.io:错误的网关

[英]Socket.io with Sails.js/Node.js and NGINX on SSL: bad gateway

I am cross posting this from Serverfault because it seems the Sails team monitors Stackoverflow. 我正在从Serverfault交叉发布此消息,因为似乎Sails团队正在监视Stackoverflow。

I just started venturing in NGINX and SSL. 我刚刚开始尝试使用NGINX和SSL。

Using Ubuntu 16.04. 使用Ubuntu 16.04。

I am running a Sails server on the standard 1337 port and just set up NGINX with SSL (using letsencrypt). 我在标准的1337端口上运行Sails服务器,只是使用SSL(使用letencrypt)设置了NGINX。 Port 80 is redirected to 443 and upstream goes to Sails. 端口80重定向到443,上游进入Sails。

I also have a Tomcat server listening on 8080 and use NGINX to redirect the same way. 我也有一个监听8080的Tomcat服务器,并使用NGINX进行相同的重定向。

Everything works fine: I can browse both servers on https without special ports on browser. 一切正常:我可以在https上浏览两个服务器,而无需在浏览器上使用特殊端口。

I have set up socket.io to use websockets protocol only (no polling). 我已将socket.io设置为仅使用websockets协议(不进行轮询)。 This is set on the server and on the browser client. 这是在服务器和浏览器客户端上设置的。

However, socket.io (sails.io) throws a 502 error no the browser. 但是,socket.io(sails.io)不会在浏览器中引发502错误。 (polling gave an error too) (轮询也给出了错误)

Here is my NGINX sites-available for the Sails server: 这是我的NGINX网站-可用于Sails服务器:

upstream sails {
   server 127.0.0.1:1337 fail_timeout=0;
}

server {
   listen 80;
   listen [::]:80;
   server_name mysails.server.com;
   return 301 https://$server_name$request_uri;
}

server {
   listen 443;
   listen [::]:443 ssl http2;
   server_name mysails.server.com;
   include snippets/ssl-mysails.server.conf;
   include snippers/ssl-params.conf;
   large_client_header_buffers 8 32k;

   location / {
      proxy_pass http://sails/;
      proxy_http_version 1.1;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header Port $server_port;
      proxy_set_header X-Real-IP $remot_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass_request_headers on;

   }

   location /socket.io/ {
      proxy_pass http://sails/;
      proxy_http_version 1.1;
      proxy_redirect off;
      proxy_set_header Host $host;
      proxy_set_header Port $server_port;
      proxy_set_header X-Real-IP $remot_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass_request_headers on;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_buffers 8 32k;
      proxy_buffer_size 64k;

   }   
}

The snippets/ssl-mysails.server.conf and snippers/ssl-params.conf files contain: snippets/ssl-mysails.server.confsnippers/ssl-params.conf文件包含:

ssl_certificate /path/to/letsencrypt/fullchain.pem;
ssl_certificate_key /path/to/letsencrypt/privkey.pem;

and

# from https://cipherli.st/
# and https://raymii.org/s/tutorials/Strong_SSL_Security_On_nginx.html

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# Disable preloading HSTS for now.  You can use the commented out header line that includes
# the "preload" directive if you understand the implications.
#add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
ssl_dhparam /etc/ssl/certs/dhparam.pem;

Anyone has any clue on what's going on...? 任何人都知道发生了什么事...?

** UPDATE ** **更新**

I have added a lot of information about logs and behaviours on the Serverfault question 我在Serverfault问题上添加了很多有关日志和行为的信息

https://serverfault.com/questions/829100/socket-io-with-sails-js-node-js-and-nginx-on-ssl-bad-gateway https://serverfault.com/questions/829100/socket-io-with-sails-js-node-js-and-nginx-on-ssl-bad-gateway

So the issue is a bad configuration on the NGINX sites-avalable conf file. 因此,问题在于NGINX站点可用的conf文件上的配置错误。

location /socket.io/ {
      proxy_pass http://sails/;
...
}

should be 应该

location /socket.io/ {
      proxy_pass http://sails/socket.io/;
...
}

Pretty basic stuff: the "location" is not forwarded to the proxy_pass (why would it be, right?) -- So you need to make sure the socket requests are redirected to the exact socket endpoint. 很基本的东西:“位置”没有转发到proxy_pass(为什么会这样,对吧?)-因此,您需要确保将套接字请求重定向到确切的套接字端点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM