Nginx config for WSS

2019-03-16 05:37发布

问题:

I am having a problem in connecting through WSS to my server. I followed the following article to setup nginx with websockets: http://www.letseehere.com/reverse-proxy-web-sockets

The following is my nginx config which serves a Play! application:

#user  nobody;
worker_processes  1;  

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

tcp {

     upstream websockets {
      ## Play! WS location
       server 127.0.0.1:9000;
     }    

    server {
        listen 80; 
        listen 8000;
        server_name socket.domain.com;

        tcp_nodelay on; 
        proxy_pass websockets;
        proxy_send_timeout 300;
    }   

     # virtual hosting
     #include /usr/local/nginx/vhosts/*;
}

http {

  server {
        listen 443 ssl;
        server_name socket.artoo.in;

        ssl_certificate      /usr/local/nginx/key/socket.domain.com.crt;
        ssl_certificate_key  /usr/local/nginx/key/socket.domain.com.key;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;

        location / {
            proxy_pass http://127.0.0.1:9000;
        }
  }
}

While the server is accessible on http://socket.domain.com, https://socket.domain.com, ws://socket.domain.com but not wss://socket.domain.com

回答1:

I was able to put together a guide in Q&A format that shows you how to do all of this with NGINX modules, much easier ;)

NGINX to reverse proxy websockets AND enable SSL (wss://)?

You will need to rebuild NGINX and follow the config in the question above.



回答2:

I have at least solved it for the short term by using stunnel (referring to this article: http://www.darkcoding.net/software/proxy-socket-io-and-nginx-on-the-same-port-over-ssl/).

Stunnel can convert HTTPS to HTTP and by that token WSS to WS. Nginx served the socket application running on 9000 port as usual:

/etc/stunnel/stunnel.conf

[https]
accept  = 443
connect = 80 
TIMEOUTclose = 0

/usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;  

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

tcp {

     upstream websockets {
      ## Play! WS location
       server 127.0.0.1:9000;
       check interval=3000 rise=2 fall=5 timeout=1000;
     }    

    server {
        listen 80; 
        listen 8000;
        server_name socket.artoo.in;

        tcp_nodelay on; 
        proxy_pass websockets;
        proxy_send_timeout 300;

    }   

     # virtual hosting
     #include /usr/local/nginx/vhosts/*;
}

#http {
#
#  server {
#        listen 443 ssl;
#        server_name socket.artoo.in;
#
#        ssl_certificate      /usr/local/nginx/key/socket.domain.com.crt;
#        ssl_certificate_key  /usr/local/nginx/key/socket.domain.com.key;
#
#        ssl_session_timeout  5m;
#
#        ssl_protocols  SSLv2 SSLv3 TLSv1;
#        ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#        ssl_prefer_server_ciphers   on;
#
#        location / {
#            proxy_pass http://127.0.0.1:9000;
#        }
#  }
#}

Now the only thing I need to worry about is how to increase the timeout for websockets on nginx, the connection seems to be breaking every 75 secs (default for nginx).