Home >Operation and Maintenance >Nginx >Nginx reverse proxy Websocket configuration tutorial to achieve real-time communication

Nginx reverse proxy Websocket configuration tutorial to achieve real-time communication

PHPz
PHPzOriginal
2023-07-04 15:28:374052browse

Nginx reverse proxy Websocket configuration tutorial to achieve real-time communication

Overview:

This article will introduce how to configure a reverse proxy through Nginx to achieve real-time communication with Websocket. Websocket is a modern network communication protocol that enables full-duplex real-time communication between clients and servers.

Background:

In the traditional HTTP protocol, the client sends a request to the server, and the connection is closed immediately after the server returns a response, so real-time communication cannot be achieved. The Websocket protocol solves this problem and achieves real-time communication between the client and the server by establishing a long-lasting, two-way connection.

Steps:

  1. Install Nginx:

First, make sure Nginx is installed on your server. If it is not installed, please use the corresponding package management tool to install it depending on the operating system.

  1. Configure Nginx reverse proxy:

Open the Nginx configuration file (usually located at /etc/nginx/nginx.conf), find the server section, and add the following configuration:

server {
    listen 80;
    server_name your.domain.com;

    location /websocket {
        proxy_pass http://backend_server:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Here, the "/websocket" path requested by the client is proxied to the 3000 port of the back-end server. At the same time, set the Upgrade and Connection headers so that Nginx supports Websocket connections.

Please make sure to replace "your.domain.com" with your own domain name and "http://backend_server:3000" with your own backend server address and port.

  1. Restart Nginx:

After saving the configuration file, execute the following command to restart Nginx:

sudo service nginx restart

In this way, Nginx will perform reverse proxy according to the configuration. Forward Websocket requests to the backend server.

Sample code:

The following is a sample code for a simple Websocket server, using Node.js and ws library:

const WebSocket = require('ws');

const wss = new WebSocket.Server({ port: 3000 });

wss.on('connection', (ws) => {
    ws.on('message', (message) => {
        console.log('Received message: ', message);
        ws.send('Server received: ' + message);
    });

    ws.on('close', () => {
        console.log('Connection closed');
    });
});

This sample code creates a Websocket server, Listen to port 3000. When a client connects successfully, it will print the received message and send the reply back to the client.

Test:

Now, you can create a Websocket connection in the client, connect to the domain name configured by Nginx, and the path is "/websocket". After initiating a message, a reply will be received from the server.

Summary:

Through Nginx's reverse proxy configuration, we can easily forward Websocket requests to the back-end server to achieve real-time communication. This provides convenience for building real-time applications with great flexibility and scalability.

Please remember to ensure the security and reliability of Nginx and backend servers in the production environment, and tune and monitor as needed. I hope this article will help you understand and use Nginx reverse proxy Websocket.

The above is the detailed content of Nginx reverse proxy Websocket configuration tutorial to achieve real-time communication. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn