Proxy WebSocket traffic with NGINX

2

You may need to keep a website and a websocket server on the same domain and port. For example, your website could be located at https://samblog.com and your websocket server at wss://samblog.com/chat

Since you can't utilize the same port by several applications, you need a proxy to route requests to a proper underlying server. You could do that easily with NGINX web server used as such a proxy. To achieve that you could setup your websocket server to listen on some local port and configure NGINX server to redirect websocket requests to that local port:

location /chat
{
    proxy_pass http://127.0.0.1:15002;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
}

location /
{
    # Your website configuration
}
Rate this post:
Share this page:

See also how to:

How to use WebSockets in web browser with JavaScript
How to install NGINX web-server on Linux and confiure its basic settings