Search code examples
phpnginxfastcgibasic-authenticationpassword-protection

Nginx Basic Auth and subfolders


I have a problem with subfolders in a basic auth procted folder. In the protected folder i have a folder named phpmyadmin, which contains phpmyadmin. Im not able to run phpmyadmin, when basic is activated. Whenn i call the the folder, i get a save-as dialog (type: application/octet-stream (18,3 KB)).

Here the important parts of mysites-available/default

location ^~ /administration/ {
    auth_basic            "Restricted Area";
    auth_basic_user_file  /var/www/myproject/sec/htpasswd;
}

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

Any ideas, how i can run php in basic-auth protected subfolders?


Solution

  • You could just repeat the location ~ \.php$ block inside the /administration/ block.

    As a workaround, I've also used this setup with success, which saves me from repeating the PHP configuration over and over in complex scenarios.

    location ^~ /administration/ {
        auth_basic            "Restricted Area";
        auth_basic_user_file  /var/www/myproject/sec/htpasswd;
        location ~ \.php$ {
            try_files /dummy/$uri @php;
       }
    }
    
    location ~ \.php$ {
        try_files /dummy/$uri @php;
    }
    
    location @php {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    

    It basically uses a named location for the PHP configuration. Unfortunately you can not use such a location everywhere. But it works with try_files. You should make sure, that there's no directory /dummy/ on your server.