Detailed explanation of Nginx forwarding socket port configuration

Detailed explanation of Nginx forwarding socket port configuration

Common scenarios for Nginx forwarding socket ports: Online learning applications, in addition to regular functions, add a chat room function, the backend selects swoole as the service provider, and does not want the front end to directly link to the service in ip:port mode, so Nginx needs to be used for forwarding.

Normally, we can directly establish a socket link on the user page, but such an operation will expose the port and bring certain security risks. Using Nginx for forwarding can hide the port. An additional problem is that some header parameters also need to be brought to the socket service provider during the forwarding process, and the rest only need Nginx to process the conversion from regular protocol to Websocket.

Among them, "Upgrade" is a hop-by-hop header and cannot be forwarded from the client to the proxy server. By forwarding the proxy, the client can use the CONNECT method to circumvent this problem. However, this does not work with a reverse proxy, since the client is not aware of any proxy server and requires special handling on the proxy server. At the same time, the hop-by-hop headers containing "Upgrade" and "Connection" cannot be passed, so you need to bring these two parameters when converting to Websocket: for example:

location /chat/ {
  proxy_pass http://backend;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

Advanced: Let the value of the "Connection" header field forwarded to the proxied server depend on the value of the "Upgrade" field in the client request header. For example:

http {
  map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
  }

  server {
    ...

    location /chat/ {
      proxy_pass http://backend;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
    }
  }

Note: The http://backend in the example is a group of load-balanced servers. If there is only a single server, you can write proxy_pass http://127.0.0.1:9501; like this.

Additionally, by default, a connection that has not transferred any data within 60 seconds will be closed, this time can be extended using the proxy_read_timeout directive. Alternatively, the proxy server can be configured to periodically send ping frames to reset the timeout and check whether the link is available.

Reference link: Nginx Websocket proxying

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the pitfalls of nginx proxy socket.io service
  • Detailed explanation of nginx websocket configuration
  • Detailed explanation of the solution to Nginx reverse proxy WebSocket response 403
  • Nginx actual combat reverse proxy WebSocket configuration example
  • Introduction to Websockets configuration method of Nginx server
  • Detailed explanation of socket segmentation in Nginx server
  • Tutorial on using Nginx as a WebSockets proxy
  • Nginx reverse proxy websocket configuration example

<<:  Common commands for mysql authorization, startup, and service startup

>>:  A brief discussion on several specifications of JS front-end modularization

Recommend

How to implement responsive layout with CSS

Implementing responsive layout with CSS Responsiv...

Introduction to deploying selenium crawler program under Linux system

Table of contents Preface 1. What is selenium? 2....

Mobile web screen adaptation (rem)

Preface I recently sorted out my previous notes o...

Solution to win10 without Hyper-V

Are you still looking for a way to enable Hyper-v...

How to implement page jump in Vue project

Table of contents 1. Create a vue-cli default pro...

How to understand SELinux under Linux

Table of contents 1. Introduction to SELinux 2. B...

Linux kernel device driver advanced character device driver notes

/****************** * Advanced character device d...

Detailed explanation of MySQL custom functions and stored procedures

Preface This article mainly introduces the releva...

Detailed explanation of how to use eslint in vue

Table of contents 1. Description 2. Download rela...

Detailed process of modifying hostname after Docker creates a container

There is a medicine for regret in the world, as l...

About MariaDB database in Linux

Table of contents About MariaDB database in Linux...

Detailed explanation of how Tomcat implements asynchronous Servlet

Preface Through my previous Tomcat series of arti...