How can you configure Nginx to handle WebSocket timeouts? nginx

How can you configure Nginx to handle WebSocket timeouts?

Nov. 13, 2023

How can you configure Nginx to handle WebSocket timeouts?

WebSocket is a communication protocol that provides full-duplex communication channels over a single TCP connection. It allows real-time data transfer between the client and server, making it suitable for applications such as chat, real-time analytics, and multiplayer games. However, sometimes WebSocket connections can experience timeouts, resulting in disrupted communication. To handle WebSocket timeouts effectively, you can configure Nginx, a popular web server and reverse proxy server, with proper timeout settings.

Step 1: Install Nginx

Firstly, you need to ensure that Nginx is installed on your server or local machine. If you don't have it installed, you can follow the official Nginx installation guide for your operating system.

Step 2: Modify Nginx Configuration

1. Locate the Nginx configuration file, typically named nginx.conf. It can be found in the /etc/nginx/ directory.

2. Open the configuration file in a text editor of your choice.

3. Locate the http block. Inside this block, add the following configuration directives:

                 upstream backend {
                    # WebSocket backend server configuration
                    server backend_server;
                 server {
                    # Nginx server configuration
                    listen       80;
                    location /websocket {
                        # WebSocket timeout configuration
                        proxy_pass http://backend;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection "upgrade";
                        proxy_read_timeout 86400s;
                        proxy_send_timeout 86400s;
                        proxy_connect_timeout 7s;
                        proxy_buffering off;

4. Save the configuration file and exit.

Step 3: Restart Nginx

After modifying the Nginx configuration file, you need to restart the Nginx service to apply the changes. You can do this by executing the following command in your terminal:

            sudo service nginx restart

Explanation of Configuration Directives

The above configuration block consists of several important directives:

  • upstream: Defines a group of backend servers to which requests will be proxied.
  • server: Defines a virtual server that listens on a specific IP and/or port.
  • proxy_pass: Passes requests to the specified backend server.
  • proxy_http_version: Sets the HTTP version for proxy requests.
  • proxy_set_header: Sets the specified request header with the given value.
  • proxy_read_timeout: Sets the maximum time in seconds allowed for reading a response from the backend server.
  • proxy_send_timeout: Sets the maximum time in seconds allowed for sending a request to the backend server.
  • proxy_connect_timeout: Sets the maximum time in seconds allowed for establishing a connection to the backend server.
  • proxy_buffering: Disables buffering of a response from the backend server.


Configuring Nginx with appropriate timeout settings for WebSocket connections can help ensure smooth and uninterrupted communication between clients and servers. By following the steps outlined above, you can handle WebSocket timeouts effectively using Nginx, enhancing the reliability and performance of your WebSocket-powered applications.