How to configure NGINX as proxy for local SMTP server?

I am doing some testing with the NodeJS smtp-server module. It exposes a listening SMTP server on any specified port. SMTP usually runs on port 25, but in order to run the Node app as a non-root user, I am prevented from binding to ports below 1000.

Since I am already using nginx on this machine, I thought the simplest way to get around this would be to bind the server internally to a higher port, for example, 2500. Then use nginx to proxy the SMTP port (25) and directly pass through traffic to the internal listening port.

How can I configure nginx to proxy/passthrough incoming SMTP connections on port 25 to the port on which smtp-server is listening? In this case, incoming TCP connections on port 25 should be directly forwarded to localhost:2500.

Note that the smtp-server app already handles SMTP authentication, TLS, etc, so nginx should not attempt to do those operations. It should just pass the traffic directly through.


I figured it out:

Edit the /etc/nginx/nginx.conf file and add the following:

stream
{
        server
        {
                listen 25;
                proxy_pass 127.0.0.1:2500;
        }
}