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 think I am getting close, but I am missing something important in nginx configuration. I would like all files ending in .php to be handled by fastcgi, so I have the following block:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

Now, in addition to executing php files, I would like all files that have a directory named _private anywhere in their path, to require basic authentication. So I have the following block:

location ~ /_private/ {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/global/.htpasswd;
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
}

I have tried with and without nesting the fastcgi block, and I have swapped the order of the statements. When I omit the whole _private location block, everything works fine (except authentication is not required). If I add the _private location block, with or without the nested .php block, the php source file is downloaded after I supply basic authentication.

What am I missing / doing wrong? Is there a better way to handle this in nginx?

share|improve this question

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.