WebSocket connection to 'ws://localhost/aa/ws1' net::ERR_CONNECTION_REFUSED

在项目中使用了websocket 进行数据推送 在本地访问没问题 部署到服务器上之后报 WebSocket connection to 'ws://localhost/aa/ws1'  net::ERR_CONNECTION_REFUSED

很是郁闷!!

后面发现服务器上使用了nginx,发现也需要在nginx中配置websocket

前台

function init(){
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://localhost/aa/ws1");
    } else if ('MozWebSocket' in window) {
        websocket = new MozWebSocket("ws://localhost/aa/ws1");
    } else {
        websocket = new SockJS("http://localhost/aa/ws1/sockjs");
    }
    websocket.onopen = function() {
        console.log('open');
    }
}

后台

registry.addHandler(new MyHandler(), "/ws1");
registry.addHandler(new MyHandler(), "/ws1/sockjs").addInterceptors(new MyHandlShakeInterceptor()).withSockJS();

nginx配置

upstream test{
    server localhost:8080;   
}
location /aa/ws1 {
    proxy_pass http://test;
    proxy_redirect off;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

 

解决~~

你可能感兴趣的:(websocket,nginx,java)