# Disable PHP in specific directories

**URL:** <https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972>\
**Category:** NGINX\
**Created:** [June 10, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972 "2010-06-10T18:00:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 6:00pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/1 "2010-06-10T18:00:58Z")

</div>

I have nginx setup with PHP as FastCGI.

On my server, I have a couple of upload (writeable) directories and I  
want to disable nginx/php from serving php files in those directories  
(and all subdirectories).

I’ve tried creating some location rules in nginx, but I can’t seem to  
get it working.

For example, if I have /public\_html/uploads

Then I have /usr1, /usr2, /usr3, etc as subdirectories under /uploads…

I want to disable php in the uploads directory and all subdirectories.  
Or, if its possible to redirect anyone that tries to access a \*.php file  
in those directories to a HTML error message file…either way.

Thanks!

Posted at Nginx Forum:

> **[Disable PHP in specific directories](https://forum.nginx.org/read.php?2,96776,96776#msg-96776)**
>
> I have nginx setup with PHP as FastCGI. On my server, I have a couple of upload (writeable) directories and I want to disable nginx/php from serving php files in those directories (and all subdirectories). I've tried creating some location rules in...

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 6:27pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/2 "2010-06-10T18:27:19Z")

</div>

You can write:  
location ^~ /uploads  
{  
root your\_root\_directory;  
}

This will disable trying to handle files in /uploads with different of  
this location.

You can also try:  
location ~\* /uploads/.\*.php$  
{  
return 403;  
error\_page 403 /403\_error.html;  
}

And put it before your location for handling php files. This will  
match all php files in uploads directory and it’s subdirs and show a  
error message.

On Thu, Jun 10, 2010 at 7:59 PM, mindfrost82 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I want to disable php in the uploads directory and all subdirectories. Or, if its possible to redirect anyone that tries to access a \*.php file in those directories to a HTML error message file…either way.

–  
Boris D..

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/3 "2010-06-10T18:45:28Z")

</div>

On Jun 10, 2010, at 6:25 PM, Boris D. wrote:

> And put it before your location for handling php files. This will  
> match all php files in uploads directory and it’s subdirs and show a  
> error message.

Just for clarification it doesn’t matter logically where he puts the  
location as long they are in the same server section. NginX uses the  
most specific match first no the first match found in the file.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 7:38pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/4 "2010-06-10T19:38:09Z")

</div>

On Jun 10, 2010, at 7:23 PM, Boris D. wrote:

> > 1. Regular expressions, in the order they are defined in the configuration file.
> > 2. If #3 yielded a match, that result is used.

Thanks for correcting me. LOL i always thought more exact match even  
with regular expressions matched first.

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 7:29pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/5 "2010-06-10T19:29:57Z")

</div>

Hello!

On Thu, Jun 10, 2010 at 8:44 PM, Rob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Just for clarification it doesn’t matter logically where he puts the  
> location as long they are in the same server section. NginX uses the most  
> specific match first no the first match found in the file.  
> [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpCoreModule#location)

No. For locations, defined with regular expressions, the first match  
is being used. This is written in you link:

> 1. Regular expressions, in the order they are defined in the configuration file.
> 2. If #3 yielded a match, that result is used.

–  
Boris D..

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 7:45pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/6 "2010-06-10T19:45:46Z")

</div>

On Thu, Jun 10, 2010 at 07:36:56PM +0200, Rob S. wrote:

> > No. For locations, defined with regular expressions, the first match  
> > is being used. This is written in you link:
> > 
> > > 1. Regular expressions, in the order they are defined in the configuration file.
> > > 2. If #3 yielded a match, that result is used.
> 
> Thanks for correcting me. LOL i always thought more exact match even with regular expressions matched first.

Could you define algorithm to find the most specific match for regexes,  
for  
example, what is more specific for “/dir/page1.php” - “^/dir/”,  
“.php$”,  
“page\d+” ?

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![mindfrost82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@mindfrost82](https://www.ruby-forum.com/u/mindfrost82)\
**Post date:** [June 10, 2010, 8:00pm UTC](https://www.ruby-forum.com/t/disable-php-in-specific-directories/189972/7 "2010-06-10T20:00:25Z")

</div>

On Jun 10, 2010, at 7:44 PM, Igor S. wrote:

> Could you define algorithm to find the most specific match for regexes, for  
> example, what is more specific for “/dir/page1.php” - “^/dir/”, “.php$”,  
> “page\d+” ?

No, but then again Igor you are way smarter then i am. NginX is a  
fantastic piece of work. Like i said earlier i just skipped that “rule”  
and more specific matches stuck in my head. how i thought about it

location ~ “^/upload/.php$” would be more exact then  
location ~ “.php$” since more parts are being explicitly matched.
