Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a site with a structure like this:

www.example.org - if you got to / you get a wordpress CMS www.example.org/wiki - a mediawiki www.example.org/project - projectpier

I'd like www.example.org/wiki and www.example.org/project to require basic auth. I know how to use basic auth, but the problem is that nginx will match the most specific rule.

So if one rule is

location ~ \.php$ {

and another is

location /wiki {

what happens is that for the .php files, the first rule is matched and they're shown regardless of the basic auth rule in the second rule. Other non .php files are protected. Obviously, this isn't what I want. The location rule for .php is to enable php-fpm.

This is quite different than apache's .htaccess, where I'd just drop a .htaccess in /wiki and it worked fine.

So...can I accomplish this in nginx? Or do I need to do something like setup wiki.example.org and project.example.org DNS aliases/virtual servers and manage it that way? That seems a long way around for what seems so simple...there must be something obviously simple that I'm missing and those smart-as-paint serverfault folk will immediately point out.

Thanks in advance!

share|improve this question

1 Answer 1

You could define separate locations for each project php files like this

location ~ /wiki.*\.php$ {

share|improve this answer
would that mean defining a separate rule for every subdirectory? for example, projectpier has a whole tree of directories off /project...and I'm sure they change when I upgrade it. And how then to define for the wordpress software in the root / ? –  raindog Apr 10 '11 at 16:49
'~ /wiki.*\.php$' will match any subdirectory under /wiki . To match root directory you could place '~ \.php$' after more specific project locations. –  AlexD Apr 11 '11 at 4:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.