Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowing only local network access in NGINX

I have a single physical server running several server blocks in nginx corresponding to different subdomains. One of them I'd like to be only accessible from devices on the same local network as the server. I know theoretically this can be done with

allow 192.168.1.0/24;
deny all;

within a location block. When I actually try to access the server from a local device, though, the request is denied. Looking at the access logs, this is because the request is shown as coming from my network's external IP rather than the device's internal IP. How can I fix this?

like image 322
Sean Avatar asked Oct 28 '25 03:10

Sean


1 Answers

Your issue is likely that you are using external DNS which routes your request to your public IP and then back to your website. Setup internal DNS and point the site resolution to the internal IP directly.

Then as you stated, you can do the following:

cat << 'EOF' >/etc/nginx/private.conf
allow 192.168.1.0/24;
deny all;
EOF

site.conf:

include                 /etc/nginx/private.conf;
like image 94
FreeSoftwareServers Avatar answered Oct 30 '25 13:10

FreeSoftwareServers