Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX bind to a specific network interface, regardless of IP address

Is there a way to make Nginx 1.11 bind to a specific interface regardless of the IP address?

I've got a home gateway to an ISP provider; it uses DHCP client to obtain its dynamic IP address. I do not know what that IP address is at NGINX configuration time.

Surely, there must be a way to make such a fine HTTP server bind to a specific network interface? I know that Apache can.

like image 358
John Greene Avatar asked Sep 16 '16 17:09

John Greene


1 Answers

Edit your startup sequence to run a command or script that captures the interface's IP address and writes it to a file in the format listen <ip>:80 or whatever port you want:

echo "listen $(ip -o -4 a s eth0 | awk '{ print $4 }' | cut -d/ -f1):80;" > /path/to/some/file

Then just have your nginx config include that file:

include /path/to/some/file;

Obviously, you'll need to make sure the IP capture occurs before the nginx startup does.

like image 143
Alex Howansky Avatar answered Sep 18 '22 21:09

Alex Howansky