nginx, only allow certain IPs to access a URL prefix

To restrict access to a specific URL prefix in Nginx based on IP addresses, you can use the allow and deny directives inside a location block. The allow directive specifies which IPs are allowed, and the deny directive blocks all others. Here’s how you can configure it:

  • Define a location block that matches your URL prefix.
  • Inside this block, use allow for each IP address you want to grant access.
  • Use deny all to block all other IP addresses.

Here’s an example config for Nginx:

server {
    # ... other server config ...

    location /your_url_prefix/ {
        allow 192.168.1.100;   # Allow this IP
        allow 192.168.1.101;   # Allow another IP
        deny all;              # Deny all other IPs

        # ... other location config ...
    }
}

In this example, replace /your_url_prefix/ with your specific URL prefix and 192.168.1.100, 192.168.1.101 with the IP addresses you want to allow. After updating the configuration, make sure to reload Nginx to apply the changes.

  • Summary: Configure Nginx to restrict access to a specific URL prefix by IP using allow and deny directives within a location block.

Read more here: Source link