Nginx can be a domain name for a separate user certification, the following practices:
1. Generate user authentication user name and password:
Copy Code code as follows:
#wget-C Soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh
Enter according to the prompt:
User name:
Password:
Filename:
The script will automatically generate the authentication file, the auth.conf content is as follows:
Copy Code code as follows:
/usr/local/nginx/conf/auth.conf
2. Add Auth authentication configuration for Nginx
Below a domain name below the auth directory for example, in the domain name of the server section add the following code:
Copy Code code as follows:
Location ^~/auth/{
Location ~. *. (PHP|PHP5)? $ {
Fastcgi_pass Unix:/tmp/php-cgi.sock;
Fastcgi_index index.php;
Include fcgi.conf;
}
Auth_basic "Authorized users only";
Auth_basic_user_file/usr/local/nginx/conf/auth.conf
}
Auth_basic_user_file as the path to the htpasswd file
3. Restart Nginx
Access http://yourdomainname/auth/prompts for a username and password.
If we only want to add user authentication for the catalog the above method does not show, below I will introduce the specific directory user authentication
Add user authentication for directory (auth Basic)
Nginx's Auth_basic authentication uses a password file that is compatible with Apache, so we need to generate a password file via Apache htpasswd.
First, find the htpasswd on your system.
Copy Code code as follows:
General CentOS will be installed in Apache, located in:
Copy Code code as follows:
If you don't find it, install it yourself.
Copy Code code as follows:
and locate the htpasswd file address.
After we find the htpasswd file, we create a user, such as this user called: Xiaoquan
Copy Code code as follows:
/usr/bin/htpasswd–c/usr/local/ngnix/conf/authdb Xiaoquan
The above command creates a user-Xiaoquan authdb password file in the Nginx profile directory, and of course you can create it somewhere else where nginx profile is easier to use.
The above command input return will be prompted to enter a password prompts the message, enter two times, you can add success.
Then modify the Nginx configuration file and add the following in a server configuration that requires Auth_basic
Copy Code code as follows:
location/admin/{
Auth_basic "Quanlei auth."
AUTH_BASIC_USER_FILE/USR/LOCAL/NGNIX/CONF/AUTHDB;
}
Finally let Nginx use the latest configuration:
Copy Code code as follows:
/usr/local/ngnix/sbin/nginx-s Reload
To add, if you use a clustered environment, you need to add Proxy_pass:
Copy Code code as follows:
location/admin/{
Proxy_pass http://cluster/mgmt/;
Auth_basic "Quanlei auth."
AUTH_BASIC_USER_FILE/USR/LOCAL/NGNIX/CONF/AUTHDB;
}