User authentication configuration in Nginx and methods to prevent users from using proxy access _nginx

Source: Internet
Author: User

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

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.