nginx和websocket

websocket是html5中用來實現長連接的一個協議。

在同時使用nginx反向代理和websocket的時候,因爲websocket的通信管道必須都要一直處於開啓狀態。

所以,有必要在nginx的配置文件中加入配置。

關鍵的地方筆者用紅色標記出來:

#user  nobody;
worker_processes  1;


#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;


#pid        logs/nginx.pid;




events {
    worker_connections  1024;
}




http {
    include       mime.types;
    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log  logs/access.log  main;


    sendfile        on;
    #tcp_nopush     on;


    #keepalive_timeout  0;
    keepalive_timeout  65;


    #gzip  on;


upstream  netitcast.com {  #服務器集羣名字   
        server    127.0.0.1:8080; 


    }    


   server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
           proxy_pass http://netitcast.com;  
            proxy_redirect default;  
      proxy_set_header Upgrade $http_upgrade;
              proxy_set_header Connection  "upgrade";

        }


        #error_page  404              /404.html;


        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}


        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}


        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }










    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}




    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;


    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;


    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;


    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;


    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


}


加上如上的兩行紅色的配置就可以放nginx支持websocket了,但是nginx的版本需在1.3以上。

但是吧項目跑起來的時候,發現還是報錯了,

a:155 WebSocket connection to 'ws://localhost/bannerWs?id=1' failed: Error during WebSocket handshake: Unexpected response code: 403

截圖如下:


經過一番搜索,原來是因爲筆者使用了Spring-websocket。

Spring 默認的情況下會設置一個webSocket客戶端的鏈接域的檢查。

org.springframework.web.socket.server.support.OriginHandshakeInterceptor
這個類來檢查客戶端連接域是否和服務器的域是否一致,不一致,服務端就返回了403這個響應碼。
我們只需要在註冊Spring webSocketHandler 的時候,加入如下代碼就可以了。
關鍵的地方,筆者用紅色標出。

webSocketHandlerRegistry.addHandler(bannerWebSocketHandler, "/bannerWs").addInterceptors(new BannerWebSocketHandShaker()).setAllowedOrigins("http://localhost");

說明允許,的域是http://localhost  ,這裏說一下筆者自己跑的Tomcat的域是http://localhost:8080

處於不同一個域,所以,之前報http響應嗎403,說是連接失敗。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章