分享下nginx配置HTTPS WSS

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/diyiday/article/details/80537345

公司有个网址有个在线聊天的业务,通过wss协议通讯,服务器基于nginx

于是在nginx中配置wss方法。

只需要添加一个location方法就行,具体代码如下:

关键点需要注意,/wss 后面不能加/ 否则返回会报错 301:

location /wss
{
        proxy_pass http://127.0.0.1:7070;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        rewrite /wss/(.*) /$1 break;
        proxy_redirect off;
}

那么你的WSS连接地址是:域名或IP/wss

workerman 连接端口是:127.0.0.1:1010;

server {

listen 443 ssl http2 default_server;
    server_name xwmm;

    ssl_certificate      key/xwmm.pem;
    ssl_certificate_key  key/xwmm.key;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
    ssl_prefer_server_ciphers  on;

index index.html index.htm index.php;
root /home/xwmm;

location /wss
{
        proxy_pass http://127.0.0.1:1010;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        rewrite /wss/(.*) /$1 break;
        proxy_redirect off;
}

location ~ .*\.(php|php5)?$
{
        #fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
        expires 30d;
}
location ~ .*\.(js|css)?$
{
        expires 1h;
}
#伪静态规则
include /home/nginx/conf/rewrite/xwmm.conf;
access_log  /log/nginx/access/xwmm.log;

}

server {
listen 80;
server_name xwmm;
rewrite ^(.*) https : //$server_name$1 permanent;
}
展开阅读全文

没有更多推荐了,返回首页