Nginx user authentication configuration (Basic HTTP authentication)
The Ngx_http_auth_basic_module module implementation allows access, and only the correct user password is entered to allow access to web content. Some of the content on the web does not want to be known by others, but wants to be seen by some people. The Nginx HTTP auth module and the Apache HTTP auth are all good solutions.
By default Nginx has installed the Ngx_http_auth_basic_module module, and if this module is not needed, you can add--without-http_auth_basic_module.
Nginx Basic AUTH Instruction
Syntax: Auth_basic string | Off
Default value: Auth_basic off;
Configuration segment: HTTP, server, location, limit_except
The default is not to open authentication, if followed by characters, these characters will be displayed in the window.
Syntax: Auth_basic_user_file file;
Default value:-
Configuration segment: HTTP, server, location, limit_except
User password file, the contents of the file are similar to the following:
Ttlsauser1:password1
Ttlsauser2:password2:comment
Nginx Authentication Configuration Instance
server{
server_name www.jb51.net jb51.net;
Index index.html index.php;
root/data/site/www.jb51.net;
Location/
{
auth_basic "nginx basic HTTP test for jb51.net";
Auth_basic_user_file conf/htpasswd;
AutoIndex on;
}
}
Note: Must pay attention to auth_basic_user_file path, otherwise will take pains to appear 403.
Generate password
You can use HTPASSWD, or use the OpenSSL
# printf "ttlsa:$ (OpenSSL passwd-crypt 123456) \ n" >>conf/htpasswd
# cat conf/htpasswd
Ttlsa: Xyjkvhxgaz8tm
Account: Ttlsa
Password: 123456
Reload Nginx
#/usr/local/nginx-1.5.2/sbin/nginx-s Reload
The effect is as follows:
Finished ~
nginx block User agent
Sometimes, you need to block some user agents from accessing the Web site, such as Ab,wget,curl, and so on, which requires the use of $http_user_agent variables.
Modify Nginx.conf
if ($http _user_agent ~* (Wget|ab)) {return
403;
}
if ($http _user_agent ~* lwp::simple| Bbbike|wget) {return
403;
}
Restart Nginx
#/usr/local/nginx-1.7.0/sbin/nginx-s Reload