centos nginx配置支持WebSocket(signalR)

signalr 默认会调用websocket去连接集线器,centos下,用nginx默认设置不支持ws的

所以,必须更改配置,让nginx通过websocket

 server {
    listen 80;
    server_name admin.mu-booking.com; 
    location / {
        proxy_pass http://127.0.0.1:5000; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    }

但是,这样问题就来了。ws的服务可以执行了。ajax的请求却不适合,毕竟ws需要的是长连接,ajax的只是短暂的,有返回后连接是会关闭的

所以,应该设置多个路径

server {
    listen 80;
    server_name admin.mu-booking.com; 
    location / {
        proxy_pass http://127.0.0.1:5000; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    location ~/Hub {
        proxy_pass http://127.0.0.1:5000; 
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
    }

 

关键 

proxy_set_header Connection upgrade,
默认key-alive的,改成upgrade

~/Hub这个路径,这个对应signalr设置的路径

 


posted @ 2019-05-06 23:44  Drek  阅读(1382)  评论(0编辑  收藏  举报