nginx websocket

 

前两天折腾了下socketio,部署完发现通过nginx代理之后前端的socket无法和后端通信了,于是暴查一通,最后解决问题:

location / {
    proxy_pass http://127.0.0.1:9999;
    proxy_connect_timeout 60;
    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-NginX-Proxy true;

    # 下面是关键
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    # 这是配置webpysessoin丢失的问题
    fastcgi_param  SCRIPT_NAME        "";
}

参考: 1. http://nginx.org/en/docs/http/websocket.html

 

http://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss

The basics of the NGINX configuration is reproduced below:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream appserver {
    server 192.168.100.10:9222; # appserver_ip:ws_port
}

server {
    listen 8888; // client_wss_port

    ssl on;
    ssl_certificate /path/to/crt;
    ssl_certificate_key /path/to/key;


    location / {
        proxy_pass http://appserver;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}




Browsing the source code and a little help from http://stackoverflow.com/questions/12102110/nginx-to-reverse-proxy-websockets-and-enable-ssl-wss#comment16394844_12102112 I was able to get it working with:

tcp {

upstream websockets {
server 127.0.0.1:55674;
check interval=3000 rise=2 fall=5 timeout=1000;

}

server {
listen 80;
server_name stomp.ideedock.local;

timeout 43200000;
websocket_connect_timeout 43200000;
websocket_read_timeout 43200000;
websocket_send_timeout 43200000;
proxy_connect_timeout 43200000;
proxy_read_timeout 43200000;
proxy_send_timeout 43200000;

so_keepalive on;
tcp_nodelay on;

websocket_pass websockets;
websocket_buffer 1k;

}

}

 

posted @ 2016-01-06 02:17  jhhu  阅读(972)  评论(0编辑  收藏  举报