An example of a way to configure user server access authentication in Nginx

  • 2020-05-10 23:30:47
  • OfStack

Nginx is super powerful and it can set up user authentication for a single domain name. The method is also very simple. We just need to generate the user authentication username and password, and then Nginx adds the auth authentication configuration.

Nginx can add user authentication for a certain domain name separately. The specific method is as follows:

1. Generate user authentication username and password:


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

Enter according to the prompt:


 User name: 
 Password: 
 The file name: 

The script will automatically generate the authentication file. auth.conf is as follows:


/usr/local/nginx/conf/auth.conf 

2. Add the auth authentication configuration for Nginx

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


location ^~ /auth/ { 
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 /usr/local/nginx/conf/auth.conf 
} 

auth_basic_user_file is the path to the htpasswd file

3. Restart Nginx

Access http://yourdomainname/auth/ will prompt you for a username and password.

If we just want to add user authentication to the directory the above method doesn't work, let me show you the specific directory user authentication.

Add user authentication to the directory (auth basic).

nginx's auth_basic authentication USES a password file that is compatible with apache, so we need to generate the password file through htpasswd of apache.

First look for htpasswd on your system:


find /  � name htpasswd 

1. CentOS will be equipped with apache in the following position:


/usr/bin/htpasswd 

If you can't find it, install it yourself:


yum install apache 

And find the htpasswd file address.

Once we find the htpasswd file, let's create a user, such as: xiaoquan


/usr/bin/htpasswd  � c /usr/local/ngnix/conf/authdb xiaoquan 

The command above creates the authdb password file for xiaoquan in the nginx configuration file directory, but you can also create the nginx password file elsewhere, where it is easier to use the nginx configuration file.

The above command will be prompted to enter the password after entering the prompt message, enter twice, you can add success.

Next, modify the configuration file of nginx, and add the following in a configuration of server that requires auth_basic:


location /admin/ { 
auth_basic "QuanLei Auth."; 
auth_basic_user_file /usr/local/ngnix/conf/authdb; 
} 

Finally, let nginx use the latest configuration:

/usr/local/ngnix/sbin/nginx -s reload 

In addition, if you are using a cluster environment, you need to add Proxy_Pass:


 User name: 
 Password: 
 The file name: 
0

PS: the method implemented using the perl script (code:)


 User name: 
 Password: 
 The file name: 
1

Give the script executable permissions:


 User name: 
 Password: 
 The file name: 
2

Script usage:


./add_user.pl
user:password

To generate user name password paste to/usr/local/nginx/conf/vhost/nginx_passwd file


Related articles: