Table Of Contents

骑驴找蚂蚁

全干工程师

通过nginx转发wss连接

再开发小程序的时候我们可能会用到websocket, 小程序的websocket必须是wss的。可能我们写websocket的服务不支持wss,这时候我们可以通过配置nginx 来转发.

比如下面的配置就是转发wss://example.com/websocket 这种连接到本地的9527端口

upstream websocket {
    server 127.0.0.1:9527 weight=1;
}

ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;


location /websocket {
    proxy_pass  http://websocket;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

留言