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:
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 [email protected]
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…
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.
http://wiki.nginx.org/NginxHttpCoreModule#location
On Jun 10, 2010, at 7:23 PM, Boris D. wrote:
- Regular expressions, in the order they are defined in the configuration file.
- 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.
Hello!
On Thu, Jun 10, 2010 at 8:44 PM, Rob S. [email protected]
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.
Module ngx_http_core_module
No. For locations, defined with regular expressions, the first match
is being used. This is written in you link:
- Regular expressions, in the order they are defined in the configuration file.
- If #3 yielded a match, that result is used.
–
Boris D…
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:
- Regular expressions, in the order they are defined in the configuration file.
- 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.
http://sysoev.ru/en/
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.