- 1 year ago
Amend nginx.conf to listen on a UDP port and forward received messages to another IP address
To configure Nginx to listen on a UDP port and forward received messages to another IP address, you need to modify the nginx.conf
file. Here's an example configuration to achieve this:
Open the
nginx.conf
file in a text editor. The file is usually located in the Nginx configuration directory, such as/etc/nginx/nginx.conf
or/usr/local/nginx/conf/nginx.conf
.Inside the
http
context, add astream
block to define the UDP listener and the upstream server to forward the messages. Here's an example configuration:nginxstream {
server {
listen 12345 udp;
proxy_pass <destination_ip>:<destination_port>;
}
}
Replace
<destination_ip>
and<destination_port>
with the IP address and port of the server where you want to forward the UDP messages.Save the
nginx.conf
file and exit the text editor.Verify the configuration for any syntax errors by running the following command:
bashnginx -t
If there are no syntax errors, you should see a message indicating that the configuration is valid.
Restart the Nginx server to apply the new configuration:
bashsystemctl restart nginx
Alternatively, you can use the appropriate command for your system to restart Nginx.
With this configuration, Nginx will listen on the specified UDP port (in this example, port 12345). Any UDP messages received on that port will be forwarded to the specified destination IP address and port using the proxy_pass
directive.
Remember to adjust the configuration according to your specific requirements, such as the UDP port, destination IP address, and port. Additionally, ensure that any necessary firewall or network configurations allow inbound UDP traffic to the Nginx server and outbound UDP traffic to the destination server.