Welcome! Log In Create A New Profile

Advanced

How do I let linux assign the port that nginx listens on?

Posted by mfkilian 
How do I let linux assign the port that nginx listens on?
September 26, 2019 06:06PM
This may seem like an odd request but I have a situation where I want to start nginx and let linux assign the port that nginx listens on. I would have thought that listen 0 default_server; would allow this but alas, no.

Is there a way to get nginx to allow linux to assign its port? (And please don't challenge why I want to do this...that's not up for debate :-)

-- Mike Kilian
Re: How do I let linux assign the port that nginx listens on?
September 27, 2019 10:38AM
The point of having nginx in any kind of scenario is to be able to talk to it, so even if this should have to be a random port, you would like to know which one it is.

Therefore, you're free to have a script determining a random port in a given range, e.g,. between 8000/tcp and 8999/tcp, and substitute that with a bit of sed in your nginx.conf.template to produce an actual nginx.conf (or some sub-configuration for a server).

You may want to apply the same mechanism (maybe based on an envsubst) to change certificates, domain names, etc., based on the type of environment you're deploying to. I use that under Terraform to deploy nginx instances depending on the target environment's parameters.

--j.
Re: How do I let linux assign the port that nginx listens on?
September 27, 2019 12:18PM
This approach is fraught with problems for picking a port. How do you pick a random port that doesn't collide with an existing port in use? How do you prevent another application from acquiring that port between the time you pick the random port (and check that it is free) and the time you actually listen on it? I've hit both of these problems in practice.

Specifying port 0 should be allowed and it should result in the OS picking the port which is far safer (though in rare cases I've seen this fail as well).

After all, port 0 was designated for this.

-- Mike
Re: How do I let linux assign the port that nginx listens on?
October 25, 2019 03:34PM
This can be done by establishing the listening port external to nginX and then using the "NGINX" environment variable to pass the file descriptor to nginX, e.g.:

NGINX="3;" nginx

You need to specify the "listen" clause(s) using the same address as was established for the listening port, e.g.:

http {
server {
listen [::]:53124;
}
}

Notes:

This mechanism is undocumented (quite deliberately, it would appear), but is widely used for systemd-based launches.

The use of the NGINX environment variable implies daemon=off.

One reason you might want to do this sort of thing is to be able to run a number of nginX instances in parallel to perform tests in isolation from each other.
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 240
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready