Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

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.