Home > Article > Operation and Maintenance > Nginx reverse proxy WebSocket configuration to achieve instant communication
Nginx reverse proxy WebSocket configuration to achieve instant communication
WebSocket is a communication protocol based on the TCP protocol. It establishes a long-term two-way connection between the browser and the server and realizes the function of real-time communication. . As a high-performance web server, Nginx can also support WebSocket through reverse proxy and achieve better load balancing and high concurrency processing.
First, to configure the reverse proxy in Nginx, you need to add a location
configuration in the nginx.conf
file. Assume that the WebSocket server is running on the local port 8000, and we reverse proxy it to the 3000 port of the Nginx server. The specific configuration is as follows:
server { listen 3000; server_name your.server.name; location / { proxy_pass http://localhost:8000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } }
In the above configuration, the proxy_pass
directive specifies The address and port of the WebSocket server to be reverse proxy. proxy_http_version
Specifies the HTTP version used by the reverse proxy, here it is set to 1.1. The proxy_set_header Upgrade
and proxy_set_header Connection
instructions tell Nginx not to modify the Upgrade
and Connection
request headers when forwarding WebSocket requests to ensure the security of the WebSocket connection. Create normally.
It should be noted that if the WebSocket server runs under the HTTPS protocol, you need to add the proxy_set_header X-Forwarded-Proto $scheme;
directive to the configuration to tell the server to use the HTTPS protocol.
After completing the above configuration, reload the Nginx configuration file to make it effective: sudo nginx -s reload
.
Next, we can use the WebSocket client to test whether the reverse proxy configuration is successful. Taking JavaScript as an example, the code is as follows:
var socket = new WebSocket('ws://your.server.name/'); socket.onopen = function() { console.log('WebSocket连接已建立'); }; socket.onmessage = function(event) { console.log('收到消息:' + event.data); }; socket.onclose = function() { console.log('WebSocket连接已关闭'); };
In the above example, we created a WebSocket object and specified the address to be connected as the address of the Nginx server. In the onopen
, onmessage
and onclose
callback functions, we can handle the events of successful WebSocket connection, message received and connection closure.
Through the above configuration and code examples, we can implement reverse proxy WebSocket on Nginx and realize the instant messaging function. Through Nginx's high performance and load balancing mechanism, we can provide stable and reliable services for WebSocket applications and handle a large number of concurrent connections. At the same time, because Nginx has mature operation and maintenance and monitoring tools, we can easily perform performance optimization and troubleshooting of WebSocket applications.
To sum up, Nginx is an excellent web server. Through reverse proxy configuration, we can easily support WebSocket and provide high-performance instant messaging services. For web applications that need to implement instant messaging functions, Nginx reverse proxy configuration is a solution worth exploring and using.
The above is the detailed content of Nginx reverse proxy WebSocket configuration to achieve instant communication. For more information, please follow other related articles on the PHP Chinese website!