I would like to use nginx to proxy an entire range (9000-9999) to the same port on a different ip address. Is it possible for nginx to automatically bind to a thousand ports? Can I do this without writing 4000 lines of nginx configuration?
You can do this with the built-in server_port variable:
That would make whatever port the request came in on be proxied to the same back-end port.
So you don't need 1000 proxy_pass statements. But, you may have a problem with the listen directive. It won't accept a range of ports, so you will need 1000 lines of the form:
That list can be easily generated with a spreadsheet. You will probably also need to adjust the number of file handles allowed by nginx to handle this number of sockets. Nginx can do it, the limits may be at the OS layer. See here and here.
I will say, though that this seems to be a problematic design. Why do you need 1000 ports? Wouldn't using host names or some part of the URL to differentiate requests make more sense? It would be far more scalable - no need for 2000 sockets as a baseline.
Here's what I ended up doing instead:
That way, you can simply connect to http://port9034.example.com;