美文网首页
nginx配置websocket wss前缀访问

nginx配置websocket wss前缀访问

作者: 闲置的Programmer | 来源:发表于2019-06-03 19:55 被阅读0次

Nginx 从 1.3 版本开始支持 WebSocket ,并且可以为 WebSocket 应用程序做反向代理和负载均衡。

在https 配置 SSL证书的localhost下面,新增一个localhost,里面写websocket的配置,如果配置了反向代理,就不需要加端口号去访问

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


server {
        listen       443;
        server_name  xxxx.xxxx.net;
        ssl                on;
        ssl_certificate /opt/ldkjdata/nginx/nginx-1.8.0/ca/xxxxx.xxxxx.net.pem ;
        ssl_certificate_key /opt/ldkjdata/nginx/nginx-1.8.0/ca/xxxx.xxxxx.net.key ;
        ssl_session_timeout  5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

        location / {
                proxy_redirect off;
                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_pass http://xxxxxx.xxxxxx.net:8080;
        }
        
        location /websocket { 
            proxy_pass http://xxxxxx.xxxxx.net:8080/websocket;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
            proxy_set_header Host $host;
            proxy_http_version 1.1; 
            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 https;
            proxy_set_header Upgrade $http_upgrade;  
            proxy_set_header Connection "upgrade"; 
            proxy_set_header Origin "";
            proxy_redirect off;
        }
        
    }

image.png

相关文章

网友评论

      本文标题:nginx配置websocket wss前缀访问

      本文链接:https://www.haomeiwen.com/subject/mhyhxctx.html