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.
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;'
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.
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!