Add the following two lines to the access directory where you want to restrict the user
The code is as follows |
Copy Code |
Auth_basic "Enter username and password"; Auth_basic_user_file nginx_passwd; |
HTPASSWD limit
On Windows and MPE platforms, passwords encrypted by HTPASSWD have a maximum length of 255 characters. A password that is too long will be truncated to 255 characters.
The MD5 algorithm used by HTPASSWD is intended for use by the Apache software, and its encrypted password cannot be used by other Web servers.
The maximum length of the user name is 255 bytes and cannot contain characters
Apache htpasswd Command Usage examples
The code is as follows |
Copy Code |
Htpasswd/usr/local/etc/apache/.htpasswd-users jsmith
|
This example adds or modifies a user's jsmith password. The program prompts for a password. If you are running on a Windows system, this password will be used specifically for
Apache modified MD5 algorithm encryption; on other platforms, the system function crypt () is used. If the file does not exist, then htpasswd does not make any exercise
Make and return an error code.
The code is as follows |
Copy Code |
Htpasswd-c/home/doe/public_html/.htpasswd Jane
|
This example creates a new file in which to store a record for the user, Jane. The program prompts for a password. If the file exists but cannot
Read or write, HTPASSWD displays a message and returns an error code.
The code is as follows |
Copy Code |
Htpasswd-mb/usr/web/.htpasswd-all Jones Pwd4steve
|
Encrypts the password (pwd4steve) from the command line using the MD5 algorithm and stores it in the specified file.
about HTPASSWD Parameters
Apache htpasswd command Option parameter description
-B
The use of batch processing mode; That is, the password is obtained from the command line without prompting for input. Use of this option should be extremely cautious because the command line
The password is clearly visible.
-C
Create a Passwdfile file. If the passwdfile already exists, it is rewritten and truncated. This option cannot be used with the-n option at the same time.
-N
Displays the results on a standard output device without updating the file. Used to generate password records that can be accepted for the Apache non-text output storage format. This election
The syntax of the item on the command line changes because the Passwdfile parameter (usually the first one) is omitted. This option cannot be used with the-C option at the same time.
-M
Use MD5 to encrypt passwords. This is the default on Windows, Netware, and TPF.
-D
Use crypt () to encrypt the password. This is the default on platforms other than Windows, NetWare, and TPF. Although it can be htpasswd on all platforms
Supported, but cannot be supported on Windows, NetWare, and TPF for the httpd server.
-S
Use Sha to encrypt passwords. It is designed for easy transfer or porting to Netscape using LDAP Directory Interchange Format (LDIF)
Of
-P
Use a plain text password. Although HTPASSWD can be established on all platforms, the httpd background is only available in Windows, NetWare, and TPF
A password that supports plain text on.
Passwdfile
The name of the file that contains the user name and password. If-C is used and the file does not exist, it is established, and if it already exists, the file is overwritten and truncated.
Username
The user name that needs to be established or updated in Passwdfile. If the username does not exist in this file, add an entry, and if it already exists, change its secret
Code.
Password
A plain-text password that will be encrypted and stored in a file. Used at the same time as the-B option only.
return value
If htpasswd succeeds in adding or modifying the username and password in passwdfile, its return value is 0 ("true"), and if an Access file occurs
Error, return 1 if there is a problem with the command-line syntax, return 2 if the password entered in the interactive state is different and two times, 3;
If the operation is interrupted, return 4, or 5 if a value is too long (user name, filename, password, or last record length); If the user name
Contains illegal characters (see Restrictions section), returns 6.
Password authentication for a directory or Web site
For example: based on the entire site certification, Auth_basic in PHP before explaining.
The code is as follows |
Copy Code |
server { Listen 80; server_name www.111cn.net 111cn.net; Root/www/akii; Index index.html index.htm index.php;
Auth_basic "Input you user name and password"; AUTH_BASIC_USER_FILE/USR/LOCAL/NGINX/CONF/VHOST/NGINX_PASSWD;
Location ~. php$ { Fastcgi_pass 127.0.0.1:9000; Fastcgi_index index.php; Include Fastcgi_params; } Location ~/.ht { Deny all; } Access_log/logs/111cn.net_access.log main; }
|
Directory-specific authentication, in a separate location, and nested in the location to explain the PHP location, otherwise the PHP file will not hold
Line and will be downloaded. Auth_basic after the nested location
The code is as follows |
Copy Code |
server { Listen 80; server_name www.111cn.net 111cn.net; Root/www/akii; Index index.html index.htm index.php;
Location ~ ^/admin/.* { Location ~. php$ { Fastcgi_pass 127.0.0.1:9000; Fastcgi_index index.php; Include Fastcgi_params; }
Auth_basic "Auth"; Auth_basic_user_file/usr/local/nginx/conf/vhost/auth/admin.pass; }
Location ~. php$ { Fastcgi_pass 127.0.0.1:9000; Fastcgi_index index.php; Include Fastcgi_params; }
Location ~/.ht { Deny all; } Access_log/logs/111cn.net_access.log main; } |
Tip: HTPASSWD uses the MD5 or system function crypt () encryption password that is specifically modified for Apache. The files managed by htpasswd can contain two
type of password; some users ' passwords are encrypted using MD5, while other users in the same file are encrypted with crypt ().