Nginx - Password Protect Not Working

2019-03-24 14:51发布

问题:

I have followed instructions and still I cant password protect my site. This is what my app-nginx.config looks like:

server {
    listen       80;
    server_name  Server_Test;
    auth_basic            "Restricted";
    auth_basic_user_file  /usr/local/nginx/conf/htpasswd;

...

}

Where am I going wrong? I copied and pasted this right from a tutorial site.

回答1:

Just made my nginx server to work, and even configured it to protect my root folder access. I'd like to share my findings with you and on the way also give a good and working answer to the question in this page.

As a new user to nginx (Version 1.10.0 - Ubuntu). The first problem I've got was to know the file locations, so here are the critical locations:

Know your locations:

Main folder location: /etc/nginx

Default site location: /var/www/ or even /ver/www/html/ (inside the html folder will be the index.html file - hope you know what to do from there.)

Configuration files:

Main configuration file: /etc/nginx/nginx.conf

Current site server conf: /etc/nginx/sites-enabled (upon first installation there is a single file there that is called default, and you'll need to use sudo to be able to change it (for example: sudo vi default)

Add password:

So, now that e know the players (for a static out-of-the-box site anyway) let's put some files in the 'html' folder and let's add password protection to it.

To setup a password we need to do 2 things: 1. create a passwords file (with as many users as we want, but I'll settle with 1). 2. Configure the current server ('default') to restrict this page and use the file in 1 to enable the password protection.

1. Let's create a password:

The line I'd like to use for this is: sudo htpasswd -c /etc/nginx/.htpasswd john (you'll get a prompt to enter and re-enter the password) of you can do it in a single line here: sudo htpasswd -c /etc/nginx/.htpasswd john [your password]

I'll explain each part of the command:

  • sudo htpasswd - do it using higher permission.
  • -c - for: create file (to add another user to an existing user skip this argument)
  • /etc/nginx/.htpasswd - the name of the file created ('.htpsswd' in the folder /etc/nginx)
  • john is the name of the user (to enter in the prompted 'user' field)
  • password is the needed password for this specific user name. (when prompted..)

Usually the htpasswd command won't work for you, so you'll have to install it's package:

Use: sudo apt-get install apache2-utils (if it fails try using sudo apt-get update and try again)

2. Let's configure the server to use this file for authentication

Let's use this line to edit the current (default) server conf file:

sudo vi /etc/nginx/sites-enabled/default (You don't have to use 'vi' but I like it..)

The file looks like this after removing most of the comments (#)

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

}

We'll need to add two lines inside the block the location ('/' points to the root folder of the site) so it'll look like this:

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;

        auth_basic "Restricted Content";
        auth_basic_user_file /etc/nginx/.htpasswd;
     }

I'll explain these new lines:

  • auth_basic "Restricted Content"; - defines the type of access management
  • auth_basic_user_file /etc/nginx/.htpasswd; - defines the file we've created (/etc/nginx/.htppasswd) as the passwords file for this authentication.

Let's restart the service and enjoy a password protected site:

sudo service nginx restart

Viola - enjoy...

Here are some more great tutorials for this:

Very good explanation

Another goo tutorial



回答2:

Make sure Nginx can access the password file. Paths for the auth_basic_user_file are relative to the directory of nginx.conf. So if your nginx.conf is located in /usr/local/nginx you can change your directive to:

auth_basic_user_file  conf/htpasswd;

and the file must be readable.

This file should be readable by workers, running from unprivileged user. E. g. when nginx run from www you can set permissions as:

chown root:nobody htpasswd_file
chmod 640 htpasswd_file

-- from http://wiki.nginx.org/HttpAuthBasicModule



标签: nginx