How to configure Nginx's socket forwarding port

PHPz
Release: 2023-05-24 11:37:06
forward
2556 people have browsed it

nginx Common scenarios for forwarding socket ports: In online learning applications, in addition to regular functions, a chat room function is added. The backend selects swoole as a service provider. At the same time, if the frontend does not want to directly link to the service through ip:port, nginx needs to be used. forward.

Under normal circumstances, 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. Others only need nginx to handle the conversion from the regular protocol to websocket.

Among them, "upgrade" is a hop-by-hop header that cannot be forwarded from the client to the proxy server. Through the forwarding proxy, the client can use the connect method to avoid this problem. However, this does not work with reverse proxies 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 header 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";
}
Copy after login

Advanced: forward to the proxy server" The value of the connection" header field depends on the value of the "upgrade" field of 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;
    }
  }
Copy after login

Note: http://backend in the example is a group of load-balanced servers. If there is only a single server, it can be written as proxy_pass http://127.0.0.1:9501;. Additionally, by default, links that do not deliver any data within 60 seconds will be closed, which can be extended using the proxy_read_timeout directive. Or the proxy server can be configured to send ping frames periodically to reset the timeout and check whether the link is available.

The above is the detailed content of How to configure Nginx's socket forwarding port. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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