Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

You can do this with the built-in server_port variable:

proxy_pass http://backend-host-name:$server_port;

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:

listen 9000;
listen 9001;
listen 9002;

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.

share|improve this answer

Here's what I ended up doing instead:

server {
    server_name "~^port(?P<forwarded_port>9\d{3})\.example\.com$";
    location / { proxy_pass$forwarded_port; }

That way, you can simply connect to;

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.