Nginx user authentication configuration method (domain name/directory)

Source: Internet
Author: User
Tags auth

Nginx can add user authentication for a domain name. The specific practices are as follows:

1. Generate the user name and password for user authentication:

The code is as follows: Copy code

# Wget-c soft.vpser.net/lnmp/ext/htpasswd.shw.bash htpasswd. sh

Enter as prompted:

User name:

Password:

File name:

The script automatically generates the authentication file. The auth. conf content is as follows:

The code is as follows: Copy code

/Usr/local/nginx/conf/auth. conf

2. Add auth authentication configuration for Nginx

The following uses the auth directory under a domain name as an example to add the following code to the server segment of the domain name:

The code is as follows: Copy code

Location ^ ~ /Auth /{

Location ~ . *. (Php | php5 )? $ {

Fastcgi_pass unix:/tmp/php-cgi.sock;

Fastcgi_index index. php;

Fcinclude GI. conf;

}

Auth_basic "Authorized users only ";

Auth_basic_user_file/usr/local/nginx/conf/auth. conf

}

Auth_basic_user_file is the path of the htpasswd file.

3. Restart Nginx

When you access http: // yourdomainname/auth/, you are prompted to enter the user name and password.


If we only want to add user authentication for the directory, the above method does not work, I will introduce the specific Directory user authentication

Add user authentication (auth basic) for the Directory)

Nginx auth_basic authentication uses apache-compatible password files. Therefore, we need to generate a password file through apache htpasswd.

First, find the htpasswd on your system.

The code is as follows: Copy code


Find/-name htpasswd

Apache is installed in CentOS generally:

The code is as follows: Copy code

/Usr/bin/htpasswd

If not, install it on your own.

The code is as follows: Copy code


Yum install apache

Find the address of the htpasswd file.

Find the htpasswd file and create a user, for example, xiaoquan.

The code is as follows: Copy code


/Usr/bin/htpasswd-c/usr/local/ngnix/conf/authdb xiaoquan

The above Command creates the authdb password file for xiaoquan in the nginx configuration file directory. Of course, you can also create it elsewhere. Here, nginx configuration files are more convenient to use.

After the preceding command is entered, the prompt message "enter password" is displayed. Enter the prompt twice to add the password successfully.

Modify the nginx configuration file and add the following content under a server configuration that requires auth_basic.

The code is as follows: Copy code

<Br/>
Location/admin/{<br/>
Auth_basic & quot; QuanLei Auth. & quot; <br/>
Auth_basic_user_file/usr/local/ngnix/conf/authdb; <br/>
} <Br/>

Finally, let nginx use the latest configuration:

The code is as follows: Copy code


/Usr/local/ngnix/sbin/nginx-s reload

In addition, if you use the cluster environment, you also need to add Proxy_Pass:

The code is as follows: Copy code

<Br/>
Location/admin/{<br/>
Proxy_pass http: // cluster/mgmt/; <br/>
Auth_basic & quot; QuanLei Auth. & quot; <br/>
Auth_basic_user_file/usr/local/ngnix/conf/authdb; <br/>
} <Br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.