keepwalking
keepwalking

Reputation: 2654

Nginx password protect

I need to password protect my folder using nginx.

I've used this rule

    location ^~ /test/
    {
            auth_basic            "Restricted";
            auth_basic_user_file  conf/htpasswd;
    }

But the problme is that if i go to /test/file.php it works, it does not requires authorization. Only /test/ asks for a password.

Upvotes: 2

Views: 2062

Answers (2)

Shri Shinde
Shri Shinde

Reputation: 309

Assuming you have set up the passwords correctly all you need to do is remove the trailing slash in your path. Also, you don't need the ^~ in the rule. Here's an example:

         location /test {
             try_files $uri $uri/ =404;
             auth_basic "Access Denied!";
             auth_basic_user_file /etc/nginx/.htpasswd;
         }

To add new users to the password use the following steps:

sudo sh -c "echo -n 'admin:' >> /etc/nginx/.htpasswd"

This will add a new user: admin to the file. To set the encrypted password for this new user use:

sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"

You will be prompted to enter the password and verify the new password. That's it. You are all set!

Upvotes: 0

liveder
liveder

Reputation: 26

I'm using it like this:

        location ~ /img/index.php {
        root   /var/www/nginx-default/phpmyedit/;
        auth_basic "phpmyedit";
        auth_basic_user_file /var/www/phpmyedit.pass;
        access_log   /var/log/nginx/img.log;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
         include /etc/nginx/fastcgi_params;
    }

Upvotes: 1

Related Questions