title: Nginx: Deny access of a directory and files inside it
body: <p>I have a directory /admin and I want to block the access of the directory and the files inside the directory whenever anyone access via public IP. Here is my setting:</p>
  
  <pre><code>location /admin/ {
     allow 192.168.0.0/24;
     deny all;
  }
  </code></pre>
  
  <p>This works fine when accessing the directory, however, if someone specifically access the file inside the directory (for example, url= "../admin/adminer.php), it doesn't deny the access of the file. I also tried other setting such as:</p>
  
  <pre><code>location ~ /admin/.*$ {
         allow 192.168.0.0/24;
         deny all;
  }
  </code></pre>
  
  <p>This seems to work in denying all the access when access from a public IP, however, the php code no longer work when accessing via internal IP, the php code simply echo out as plaintext. </p>
  
  <p>The rest of my location directives is provided here in case it somehow affect the behaviours:</p>
  
  <pre><code>location / {
     try_files $uri $uri/ /index.php?args;
  }
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
  
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 60m;
  }
  </code></pre>
  
  <p>Hope someone can help me to solve this.</p>