I have an Nginx webserver within a LAN which is being addressed from the internet by NAT. The variable $server_addr
contains the internal LAN address of the server. Is there a way to map the external (internet-exposed) IP address of the webserver to an Nginx variable?
nginx binds to all interfaces by default, it does not care about the interface IP addresses. So, it serves to any IP address, whether request comes via WAN or LAN.
You need to set up a dynamic DNS for your domain. Your server would contain a daemon that updates the new external IP to the dynamic DNS provider. Then all clients would use the dynamic DNS name to connect to the server.
Another alternative is to set up a DNS server in LAN, which would resolve the server's domain name to the LAN IP address, and then make LAN clients use that DNS server.
I have solved the issue with a cronjob to update a mapping file for the external IP address: