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 nginx with php-fpm installed on Debian Squeeze.
Directory tree is:

  • /var/www/mysite
    • index.php
    • secret_folder_1
      • admin.php
      • static.html
    • secret_folder_2
      • admin.php
      • static.html
    • pictures
    • img01.jpg

I need to close secret_folder_1 and secret_folder_2 with basic_auth. Now config looks like:

location ~ /secret_folder_1/.+\.php$
{
        root /var/www/mysite/;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  /var/www/mysite$fastcgi_script_name;
        include        fastcgi_params;
        auth_basic "Restricted Access";
        auth_basic_user_file /path/to/.passwd;
}

location ~ /secret_folder_1/.*
{
        root /var/www/mysite/;
        auth_basic "Restricted Access";
        auth_basic_user_file /path/to/.passwd;
}

Same config for secret_folder_2.

Is it normal? I mean, first location for serving php files in restricted folder, and second location for serving static files.

Can it be simplified?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

The nested locations may be better:

location /secret_folder_1 {
    root /var/www/mysite/;
    auth_basic "Restricted Access";
    auth_basic_user_file /path/to/.passwd;

    location ~* \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi.conf;
    }
}

Please notice that you should define SCRIPT_FILENAME parameter with $document_root variable instead of hardcoded /var/www/mysite/:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
share|improve this answer
    
More importantly, root must be set before $document_root is used, otherwise $document_root is empty. –  Lekensteyn Feb 15 '12 at 13:07

Your Answer

 
discard

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.