title: allow localhost; deny all; for all despite "index.php" and "/"
body: <p>I'm trying to restrict direct access to .php files on web server.</p>
  
  <p>Used <code>allow localhost;deny all;</code>. However, this restricts access also to <code>index.php</code>.</p>
  
  <p>How to overcome this issue? Is there something like <code>IF conditions</code>?</p>
  
  <p>My config:</p>
  
  <pre><code>if ($request_uri ~* "^(/)index\.php$") {
      return 301 $1;
  }
  
  location / {
  try_files $uri $uri/ /index.php?$args; 
  
      rewrite ^/(\w+)$       /?system=$1       break;
      rewrite ^/(\w+)/(\w+)(/.)*$ /?system=$1&amp;id=$2 break;
      rewrite ^/(.*)/$ /$1 permanent; 
  
  location ~ \.php$ {
         try_files  $uri =404;
  
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         #fastcgi_pass 127.0.0.1:9000;
         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
         fastcgi_index index.php;
         include fastcgi_params;
  
         fastcgi_param PHP_VALUE "auto_prepend_file=/usr/share/nginx/html/web/config.php";
      }
  
  }
  </code></pre>