nginx configuration supports websocket forwarding

Method 1. Dynamically determine whether the websocket protocol needs to be upgraded

Edit nginx.conf and be sure to add the following configuration in the http area:

http {

    #Custom variable $connection_upgrade

    map $http_upgrade $connection_upgrade {

        default keep-alive; #The default is keep-alive which can support general http requests

        'websocket' upgrade; #If it is websocket, it is upgradeable.

    }

}

The function of map instruction:

This function is mainly to construct and change the value of $connection_upgrade based on the value of $http_upgrade in the client request, that is, to create a new variable $connection_upgrade based on the value of the variable $http_upgrade.

The created rule is what is inside {}. If $http_upgrade is websocket, then the value of $connection_upgrade is upgrade to support the upgrade protocol. Otherwise keep-alive

server {
   server_name test.server;
   listen 8083;
   listen [::]:8083;
 

   location / { //default match

            proxy_pass http://$httpUpstreamName_8083;

            #Nginx proxy webSocket often interrupted (timeout) solution

            proxy_connect_timeout 4s;                

            proxy_read_timeout 60s; #If it doesn't work, you can consider setting this time longer

            proxy_send_timeout 12s;                

            #Upgrade http1.1 to websocket protocol

            proxy_http_version 1.1;

            proxy_set_header Upgrade $http_upgrade;

            proxy_set_header Connection  $connection_upgrade;

    }

}

Test whether ws connection forwarding is normal: ws://test.server:8083/

Method 2. Directly code and upgrade websocket

server {
   server_name test.server;

   listen 80;
   listen [::]:80;
   listen 8083;
   listen [::]:8083;
   listen 8088;
   listen [::]:8088;

    location / { #Can only be configured through $serviceName $httpUpstreamName_xxx may not exist

        if ($server_port = "80") { #sys default require

            proxy_pass http://$serviceName:80;

        }

        if ($server_port = "8080") { #sys default require

            proxy_pass http://$serviceName:8080;

        }

        if ($server_port = "8088") { #custom

            proxy_pass http://$serviceName:8088;

        }

        proxy_pass http://$serviceName:8083;

        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;

        proxy_set_header Connection "upgrade";

    }

}

Test whether ws connection forwarding is normal: ws://test.server:8083/

Further reading:

In nginx, each server block should have a unique server_name to avoid conflicts, and duplicate server_names are not allowed in different server blocks.

The set command is used to set variables , set $XX=XX, the variables set in the server block are only valid in the current server block.

In the HTTP protocol, keep-alive, upgrade and close are three different connection management methods. The differences between them are as follows:

keep-alive: This is the default connection management method for HTTP. When using keep-alive, multiple requests and responses are allowed to reuse the same TCP connection. This reduces the overhead of establishing and closing connections and improves performance. Multiple requests on the same connection will be sent and received consecutively until the connection's maximum idle time is reached or an explicit close connection instruction is received.

upgrade: This is a special connection upgrade method, usually used to support protocol upgrades, such as from HTTP to WebSocket. When a client sends a request with an "Upgrade" header, the server can upgrade the connection to another protocol with custom capabilities. During the upgrade process, a protocol switch and other necessary handshakes are usually negotiated between the server and client.

close: When the server or client wishes to terminate the connection, it can send a request or response with the "Connection: close" header. This instructs the other party to close the connection. In this case, regardless of whether keep-alive was previously enabled, the connection will be closed and cannot be reused.

Guess you like

Origin blog.csdn.net/qq_42152032/article/details/132533774