Add htpasswd authentication for Nginx
I. Requirement Description
Nginx serves as a web server. Because the business needs to access the website, add a layer of authentication information to prevent people outside the company from accessing the website. The requirement is similar to that of Apache to add access verification for the specified directory. Generally, htpasswd is used in Apache to add access verification. Add Nginx.
2. Compile the following program
Environment: perl must be supported. (Because perl is used)
Vi htpasswd. sh
#! /Bin/bash
PATH =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export PATH
Echo "=========================================="
Echo "# A tool like htpasswd for Nginx #"
Echo "#-----------------------------------#"
Echo "# Author: Licess http://blog.csdn.net/longxibendi #"
Echo "=========================================="
# Set UserName
Username = ""
Read-p "Please input UserName:" username
If ["$ username" = ""]; then
Echo "Error: UserName can't be NULL! "
Exit 1
Fi
Echo "==============================="
Echo "UserName was: $ username"
Echo "==============================="
# Set password
Unpassword = ""
Read-p "Please input the Password:" unpassword
If ["$ unpassword" = ""]; then
Echo "Error: Password can't be NULL! "
Exit 1
Fi
Echo "==============================="
Echo "Password was: $ unpassword"
Echo "==============================="
Password = $ (perl-e 'print crypt ($ ARGV [0], "pwdsalt") '$ unpassword)
# Set htpasswd file
Htfile = ""
Read-p "Please input Auth filename:" htfile
If ["$ htfile" = ""]; then
Echo "Error: Auth filename can't be NULL! "
Exit 1
Fi
Echo "==============================="
Echo "Auth File:/longxibendi/nginx/conf/$ htfile"
Echo "==============================="
Get_char ()
{
SAVEDSTTY = 'stty-G'
Stty-echo
Stty cbreak
Dd if =/dev/tty bs = 1 count = 1 2>/dev/null
Stty-raw
Stty echo
Stty $ SAVEDSTTY
}
Echo ""
Echo "Press any key to Creat... or Press Ctrl + c to cancel"
Char = 'get _ char'
If [! -F/longxibendi/nginx/conf/$ htfile. conf]; then
Make-p/longxibendi/nginx/conf/$ htfile. conf
Echo "Create Auth file ......"
Cat>/longxibendi/nginx/conf/$ htfile. conf <eof
$ Username: $ password
Eof
Echo "Create Auth file successful, auth file path:/longxibendi/nginx/conf/$ htfile. conf ."
Else
Echo "File already exists, please run this script again ."
Exit 1
Fi
3. generate user name and password information
Bash htpasswd. sh
Enter the user name, password, and authentication file name as prompted. The script automatically generates the authentication file. Record the file path returned by the script. For example:/longxibendi/nginx/conf/longxibendi. auth. conf
4. Modify the Nginx. conf configuration file and add the auth authentication configuration for Nginx
Location ^ ~ /Longxibendi/
{
Auth_basic "Authorized users only ";
Auth_basic_user_file/longxibendi/nginx/conf/longxibendi. auth. conf; # enter the file path returned by the preceding script;
}
Or add it after location, for example
Location /{
Index. php index.html index.htm;
If (! -F $ request_filename ){
Rewrite ^/(. *)/iphone/index. php? $1 last;
}
}
Location ~ . * \. (Php | php5) $ {
Fastcgi_pass 127.0.0.1: 8000;
Fastcgi_index index. php;
Fcinclude gi. conf;
}
Auth_basic "Authorized users only ";
Auth_basic_user_file/longxibendi/nginx/conf/moyi.365.auth. conf;
5. Restart Nginx
Cd sbin/nginx;./nginx-s reload
Vi. Precautions
Enter the user name and password when accessing Each browser for the first time. You only need to enter it once and then open the browser.