# Question about multiple applicable locations in a server config

**URL:** <https://www.ruby-forum.com/t/question-about-multiple-applicable-locations-in-a-server-config/235327>\
**Category:** NGINX\
**Created:** [June 13, 2013, 9:31pm UTC](https://www.ruby-forum.com/t/question-about-multiple-applicable-locations-in-a-server-config/235327 "2013-06-13T21:31:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [June 13, 2013, 9:31pm UTC](https://www.ruby-forum.com/t/question-about-multiple-applicable-locations-in-a-server-config/235327/1 "2013-06-13T21:31:49Z")

</div>

I have read documentation from several sources, and there is still 1  
detail about nginx configuration that I do not fully understand, and  
would like some additional guidance. In the nginx location  
configuration, how does one handle locations that would match multiple  
criteria, but nginx can only match on one?

For example, lets say I have a php application and I am using php-fpm,  
which has an admin interface that I want to protect using htaccess. I  
would have location directives in my config with the following contents:

location ~\* /admin/ {  
auth\_basic “Restricted”;  
auth\_basic\_user\_file htpasswd;  
}

location ~\* .php$ {  
try\_files $uri =404;  
fastcgi\_index index.php;  
fastcgi\_pass unix:/var/run/php-fpm/php-fpm.sock;  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;  
}

From my understanding of how nginx processes the config file, it would  
match the /admin/ regex first, but what if the files contained in this  
directory are PHP scripts? Since the first location matches in my  
config, I believe that the second location with the PHP redirect is  
never processed?

Thanks in advance,

-JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [June 13, 2013, 10:48pm UTC](https://www.ruby-forum.com/t/question-about-multiple-applicable-locations-in-a-server-config/235327/2 "2013-06-13T22:48:55Z")

</div>

On 13 June 2013 20:31, John B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> auth\_basic “Restricted”;  
> }
> 
> From my understanding of how nginx processes the config file, it would match  
> the /admin/ regex first, but what if the files contained in this directory  
> are PHP scripts? Since the first location matches in my config, I believe  
> that the second location with the PHP redirect is never processed?

Remember you can use nested location. I would imagine (though haven’t  
tested) that this will work:

* * *

location ~ .php {  
try\_files $uri =404;  
fastcgi\_index index.php;  
fastcgi\_pass unix:/var/run/php-fpm/php-fpm.sock;  
include fastcgi\_params;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
fastcgi\_param SCRIPT\_NAME $fastcgi\_script\_name;

location ~ /admin/ {  
auth\_basic “Restricted”;  
auth\_basic\_user\_file htpasswd;  
}  
}
