How do I password protect a folder?


I’m trying to protect a folder on my server and have added the following to my server block in my site config:

location ^~  /hiddenfolder/ {
    auth_basic            "Restricted";
    auth_basic_user_file  /var/www/;

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

I have created the .htpasswd file with user and password, so that should work.

I would also like to make sure that this folder is not being cached.

I’m not getting it to work though. This is not part of WordPress, but another folder on my server. Also have cloudflare installed, not sure if that’s a problem?


// Jens.

Some updates: Doesn’t seem to be a cloudflare issue. When using the above code, I get a 502: Bad Gateway error.

If instead using this code for location php:

location ~ .php {

fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;


…I’m getting a file not found error.

// Jens.

try removing this line

Also you have used different sockets for passing fastcgi intercepts to php. please check the value for listen variable in /etc/php5/fpm/php-fpm.conf and use the same value.And then you method should work.

It’s been a long time, and we haven’t heard from you. It looks like your issue is resolved.

I am closing this support topic for now. Feel free to create a new support topic if you have any queries further. :slight_smile: