配置 nginx 以支持 websocket


如果 websocket 中間有反向代理服務器,是不能直接通信的,需要進行配置,以下配置均在 nginx.conf 文件中進行。

首先在 http 節點下新增:

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

然后在相應的 server 節點下新增:

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

    server {
        listen       8020;
        server_name  localhost;

        location / {
            proxy_pass http://localhost:8010;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
        }
    }
}

大概是這樣,然后就可以了~

參考文檔:https://www.nginx.com/blog/websocket-nginx/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM