Home  >  Article  >  Operation and Maintenance  >  How to implement Nginx TCP/UDP proxy configuration

How to implement Nginx TCP/UDP proxy configuration

王林
王林Original
2023-11-08 15:14:072110browse

How to implement Nginx TCP/UDP proxy configuration

Nginx is a powerful web server software that can not only handle HTTP/HTTPS protocol requests, but also forward other protocols through TCP/UDP proxy.

Below, we will introduce how to implement TCP/UDP proxy configuration through Nginx and provide specific code examples.

  1. Configuring TCP proxy

First, add the following code to the Nginx configuration file:

stream {
    server {
        listen        80;
        proxy_pass    backend:8080;
    }
}

This configuration implements all 80 ports TCP requests are forwarded to port 8080 of the backend host. It should be noted that the backend can be an IP address or the name of another server in the Nginx configuration file.

In addition, in order for Nginx to monitor TCP requests, you need to add the -g 'daemon off;' option to the command to start Nginx. The complete startup command is as follows:

nginx -g 'daemon off;'
  1. Configure UDP proxy

For UDP proxy, Nginx needs to use the third-party module Stream Module. You need to add the --with-stream option when compiling Nginx, and add the following code to the Nginx configuration file:

stream {
    server {
        listen          53 udp;
        proxy_pass      backend:53;
        proxy_timeout   1s;
        proxy_responses 1;
    }
}

This configuration forwards all UDP port 53 requests to 53 of the backend host. On the port, a timeout of 1 second is set and a maximum of one response result is returned.

It should be noted that in the above configuration, backend can be an IP address or the name of another server in the Nginx configuration file.

  1. Complete example

Finally, we provide a complete TCP and UDP proxy configuration example, the code is as follows:

events {}

http {}

stream {
    server {
        listen        80;
        proxy_pass    backend:8080;
    }

    server {
        listen          53 udp;
        proxy_pass      backend:53;
        proxy_timeout   1s;
        proxy_responses 1;
    }
}

It should be noted that the above The backend in the example can be an IP address or the name of another server in the Nginx configuration file.

Summary

Through the above example code, we have learned how to implement TCP/UDP proxy configuration in Nginx. The key points involved include using the Stream Module module, adding keywords such as listen and proxy_pass, etc. . I hope this article can provide some help for everyone to implement Nginx proxy configuration!

The above is the detailed content of How to implement Nginx TCP/UDP proxy configuration. 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