mapping the external WAN address to a Nginx variable

Solution 1:

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.

Solution 2:

I have solved the issue with a cronjob to update a mapping file for the external IP address:

#!/usr/bin/env bash
# enumerate authorized networks
cat > /etc/nginx/current_ip.conf <<EOF
geo \$mysite_geo {
 default        "no";
 127.0.0.1      "yes";  # network which should have access
 10.10.0.0/16   "yes";  # network which should have access
 EOF

((echo "   " && (dig +short example.com | grep '^[.0-9]*$')) \
 && echo "\"yes\"; }  # current dynamic ip") |xargs -d'\n' \
 >> /etc/nginx/current_ip.conf