# Effectiveness of multiple locations and less if directives

**URL:** <https://www.ruby-forum.com/t/effectiveness-of-multiple-locations-and-less-if-directives/201520>\
**Category:** NGINX\
**Created:** [January 10, 2011, 1:10am UTC](https://www.ruby-forum.com/t/effectiveness-of-multiple-locations-and-less-if-directives/201520 "2011-01-10T01:10:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 10, 2011, 1:10am UTC](https://www.ruby-forum.com/t/effectiveness-of-multiple-locations-and-less-if-directives/201520/1 "2011-01-10T01:10:11Z")

</div>

Hello everybody,

I would like to restrict the direct access for instance, to \*.txt files  
except sitemap.txt and robots.txt. And no direct access also to any  
\*.php files except index.php and upgrade.php. I also would like to  
prevent the “try\_files” to discover and serve any \*.php files as they  
should be passed on to fastcgi backend. According to the recommendation,  
I should use as less “if” directive inside “location” as possible. So I  
ended up with the settings similar like below. My actual settings are  
longer than that.

If I would combine some “location” directives into one, but use more  
“if” directives and regular expressions, would that make nginx performs  
the processing ineffectively which in the end makes it slower?

Kind regards,

Anto

=================================================================  
server {

```
   server_name mysite.eu;
   root /home/www/mycms;
   access_log /home/www/mycms/myvhost1/logs/nginx_access.log;
   valid_referers mysite.eu *.mysite.eu;

   if ( $request_method !~ ^(GET|HEAD|POST)$ ) {
      return 410;
   }

   location = /favicon.ico {
      rewrite /favicon.ico /myvhost1/favicon.ico break;
      expires max;
      log_not_found off;
      access_log off;
   }

   location = /sitemap.txt {
      rewrite /sitemap.txt /myvhost1/sitemap.txt break;
      log_not_found off;
      access_log off;
   }

   location = /robots.txt {
      log_not_found off;
      access_log off;
   }

   location = / {
      rewrite ^ /index.php?arg=page/0 last;
   }

   location ~* \.(txt|log|php|inc|sh|pl|py)$ {
      return 410;
      log_not_found off;
   }

   location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
      if ($invalid_referer) {
         return 410;
      }
      expires max;
      log_not_found off;
      access_log off;
   }

   location / {
      try_files $uri @mod_uri;
   }

   location @mod_uri {
      rewrite ^/(.*)$ /index.php?arg=$1 last;
   }

   location = /upgrade.php {
      if ($invalid_referer) {
         return 410;
      }
      include fastcgi_params;
      fastcgi_pass unix:/tmp/fastcgi.sock;
   }

   location = /index.php {
      include fastcgi_params;
      fastcgi_pass unix:/tmp/fastcgi.sock;
   }
}

```

=================================================================

---

<div class="post-metadata">

**Author:** ![Anto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anto](https://www.ruby-forum.com/u/Anto)\
**Post date:** [January 14, 2011, 8:04pm UTC](https://www.ruby-forum.com/t/effectiveness-of-multiple-locations-and-less-if-directives/201520/2 "2011-01-14T20:04:08Z")

</div>

I am sorry for being a bit pushy, but it would be great if I could get  
an answer for my question.

---

<div class="post-metadata">

**Author:** ![Anto](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@Anto](https://www.ruby-forum.com/u/Anto)\
**Post date:** [January 14, 2011, 8:41pm UTC](https://www.ruby-forum.com/t/effectiveness-of-multiple-locations-and-less-if-directives/201520/3 "2011-01-14T20:41:15Z")

</div>

On 14 Jan 2011 19h03 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > files as they should be passed on to fastcgi backend. According to
> > 
> > if ( $request\_method !~ ^(GET|HEAD|POST)$ ) {  
> > location = /sitemap.txt {  
> > location = / {  
> > return 410;  
> > location @mod\_uri {
> > 
> > location = /index.php {  
> > include fastcgi\_params;  
> > fastcgi\_pass unix:/tmp/fastcgi.sock;  
> > }  
> > }

This is how I do it in my drupal config:

The last location stanza is:

## Any other attempt to access PHP files returns a 404.

location ~\* ^.+.php$ {  
return 404;  
}

Other PHP/FastCGI locations appear before that enumerate _all_  
accessible PHP files.

Cf. [GitHub - perusio/drupal-with-nginx: Running Drupal using nginx: an idiosyncratically crafted bleeding edge configuration. · GitHub](https://github.com/perusio/drupal-with-nginx) for details.

— appa
