Nginx配置websocket的反向代理

由于一般会有跨域问题,就直接把跨域也一并配置了。Nginx的跨域配置详情可以参考我之前的文章:Nginx配置跨域请求

websocket的反向代理配置:

直接贴代码:


server {
    listen 9000; # 监听9000端口
    server_name   websocket_server;
    
    # 允许跨域
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    if ($request_method = 'OPTIONS') {
        return 204;
    }

    location / {
        #添加wensocket代理
        proxy_pass http://127.0.0.1:9093;  # websocket服务器。不用管 ws://
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
    原文作者:Developer
    原文地址: https://segmentfault.com/a/1190000012925329
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞