• notice
  • Congratulations on the launch of the Sought Tech site

Add support for WebSocket in Nginx configuration

    Nginx has supported WebSocket since version 1.3, and can also do reverse proxy and load balancing for WebSocket applications. Websocket uses ws or wss as a unified resource identifier, similar to HTTP or HTTPS. The default Websocket ws protocol uses port 80; when running on TLS, wss means Websocket on TLS, which is equivalent to HTTPS. The wss protocol uses port 443 by default. Wss is the secure transmission of ws based on SSL.

    To configure Nginx domain name to support WSS, you only need to add two lines of configuration, Upgrade, as follows:

location /uri {
    proxy_pass http://127.0.0.1;
    proxy_http_version 1.1;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

    WebSocket is a protocol for full-duplex communication over a single TCP connection, allowing the server to actively push data to the client. In the WebSocket API, the client and the server only need one handshake to create a persistent connection and perform two-way data transmission. Although WebSocket and HTTP are two different protocols, the handshake mechanism of WebSocket and HTTP is the same. It uses the Upgrade protocol header in HTTP to upgrade the connection from HTTP to WebSocket. When the client sends a Connection: Upgrade request header, The Nginx proxy server needs to explicitly set the Connection and Upgrade header information, and use 101 (exchange protocol) to return a response, and establish a tunnel between the client, the proxy server, and the backend server to support WebSocket. WebSockets are still affected by Nginx's default proxy_read_timeout of 60 seconds. Therefore, if you have a program that uses WebSockets and may not send any data for more than 60 seconds, you need to increase the timeout setting, and you can also implement a regular ping mechanism to keep the connection.



Tags

Technical otaku

Sought technology together

Related Topic

1 Comments

author

brand atorvastatin 40mg & lt;a href="https://lipiws.top/"& gt;buy lipitor 80mg pill& lt;/a& gt; atorvastatin 20mg without preion

Akqqgr

2024-03-08

Leave a Reply

+