Baishun's Space

How to set nginx with websocket

By Baishun on Feb 7, 2021
nginx websocket

to handle a websocket in nginx is very easy, first we create an upstream in http scope

upstream socket-end{
    server ${serverIP}:4000;# this is a websocket server create with maybe socket.io
}

next we config in server scope like this

server{
    location /socket/ {
        rewrite /socket/(.*) /$1 break;
        access_log /var/log/nginx/socket.access.log;
        error_log /var/log/nginx/socket.error.log;
        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://socket-end;
    }
}

Perfect!

For business cooperation or you have any questions, please send email to : lecy.cc.app@gmail.com
© Copyright 2024 by Baishun Space. Built with ♥ by Lecy. Origin theme of this blog is from ixartz. Social Icons are copied from astro-social-share