Nginx reverse proxy WebSocket configuration to achieve real-time communication

WBOY
Release: 2023-07-04 17:37:36
Original
2875 people have browsed it

Nginx reverse proxy WebSocket configuration to achieve real-time communication

WebSocket is a network protocol that supports full-duplex communication. It can establish a persistent connection between the client and the server to achieve real-time communication. Nginx is a high-performance web server and reverse proxy server. Through the reverse proxy configuration of Nginx, you can proxy WebSocket requests to the back-end server, thereby realizing the real-time communication function of WebSocket.

Here is an example on how to configure Nginx reverse proxy WebSocket:

  1. Install Nginx
    First, we need to install Nginx on the server. You can use package management tools (such as apt, yum, etc.) to install. After the installation is complete, you can check the installed version of Nginx by running the "nginx -v" command.
  2. Configure Nginx
    Configure Nginx's reverse proxy so that it can forward WebSocket requests to the backend server. Open the Nginx configuration file, usually located under the path "/etc/nginx/nginx.conf" or "/etc/nginx/conf.d/default.conf", and add the following configuration:
server {
    listen 80;
    server_name your_server_domain;

    location / {
        proxy_pass http://your_backend_server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
Copy after login

Among them, your_server_domain is the domain name or IP address of the server, and your_backend_server is the address and port of the backend server.

  1. Restart Nginx
    After completing the configuration, you need to restart the Nginx server for the configuration to take effect. You can use the following command to restart Nginx:
sudo service nginx restart
Copy after login
  1. Backend Server
    On the backend server, you need to write code that can handle WebSocket requests. Here we take Node.js as an example to create a simple WebSocket server:
const WebSocket = require('ws');

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

wss.on('connection', ws => {
    console.log('New client connected');

    ws.on('message', message => {
        console.log(`Received message: ${message}`);
    });

    ws.on('close', () => {
        console.log('Client disconnected');
    });
});
Copy after login

The above code creates a WebSocket server, prints a log when each new client connects, and receives and print relevant information respectively when closing the connection.

  1. Test
    Now, we can use WebSocket client tools (such as browser developer tools, Postman, etc.) to test the real-time communication function of WebSocket. By sending a WebSocket request to the Nginx server, Nginx forwards the request to the backend server for processing.

Through the above steps, we successfully configured the reverse proxy of Nginx and implemented the real-time communication function of WebSocket.

Summary: Nginx reverse proxy WebSocket configuration can proxy WebSocket requests to the back-end server, making real-time communication possible. Through the above steps, we can easily configure Nginx to support WebSocket and write code on the backend server to handle WebSocket requests. In this way, we can use the high performance and stability of Nginx to achieve real-time communication functions.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template