The Nginx server sets methods to verify access to the site

  • 2020-05-09 19:56:54
  • OfStack

After setting up the access verification for the website directory, we need to enter the authentication password to enter this page. Next, I will introduce the summary of the method of configuring the directory access verification code in nginx.

1. Create class htpasswd file

Perform:
 


wget -c soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh 

Press the prompt to enter the user name, password, and authentication file name. The script generates the authentication file automatically. Record the path to the file that the script returned. Such as: / usr/local/nginx/conf/vpser net. auth.

2. Add auth authentication configuration for Nginx

Take the soft directory below a domain name as an example. Add the following code to the server section of the domain name:
 

 
location ^~ /soft/
{
auth_basic "Authorized users only";
auth_basic_user_file // Here write the file path that the previous script returned ;

Authorized users only is the prompt message, which can be modified to the message you want him to prompt; After auth_basic_user_file, you need to fill in the path to the home file returned by the htpasswd.sh script. Once you have changed the configuration, restart nginx and access http://yourdomainname/soft/ will prompt you for your username and password.

Note that PHP in this directory will not be parsed after the authentication is added, and a download prompt will appear. If you want to parse PHP, you can change the above configuration to:

 


location ^~ /soft/ {
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 // Here write the file path that the previous script returned ;
}


Related articles: