How do I get socket.io running for a subdirectory

前端 未结 4 1873
广开言路
广开言路 2021-02-02 10:46

I\'ve got a proxy running that only hits my node.js server for paths that being with /mysubdir How do I get socket.io configured for this situation?

In my c

4条回答
  •  不思量自难忘°
    2021-02-02 11:06

    Using nginx, this a solution without the need to change anything in the socket.io server app:

    In the nginx conf:

    location /mysubdir {
       rewrite ^/mysubdir/(.*) /socket.io/$1 break;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
       proxy_http_version 1.1;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_set_header Host $host;
       proxy_pass http://127.0.1.1:3000;
    }
    

    In the server:

    var io = require('socket.io')(3000)
    

    In the client:

    var socket = io.connect('https://example.com/', {
        path: "/mysubdir"
    })
    

提交回复
热议问题