Java配置19-配置Nginx代理websocket請求

1.需求

前後端要求實時交互,互相通信,使用了websocket。

2.實現

修改nginx配置文件nginx.conf

1)在http塊中添加以下參數

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

 

2)在location中 添加以下參數

        location /a/b/ {
            proxy_pass   http://ip:port/c/d;
            proxy_http_version 1.1;
            proxy_connect_timeout 40s;
            proxy_read_timeout 60s;
            proxy_send_timeout 12s;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }


/a/b---前端請求

http://ip:port/c/d---後端接口地址

3.其他

其他資料,以供參考

nginx官方文檔 

Nginx代理webSocket時60s自動斷開, 怎麼保持長連接

Nginx(6) nginx反向代理配置websocket,以及配置後websocket連接後立即斷開的解決方法

 

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