Methods of payment Abuse

How to configure Auth Basic NGINX

14.03.2021, 22:14

You may have never given much thought to what Auth Basic is, but you've definitely encountered it when going into your router's settings. This is what they call the mechanism of authorization by username and password at the web server level. Such authorization is supported in Apache and Nginx. In this instruction we suggest to consider such a question as how to configure Auth Basic for a specific route or a specific site.

Configuring Auth Basic in NGINX

This is what the Auth Basic authorization window looks like:

Окно авторизации Basic Auth

Now you should understand what this instruction is about. It is possible to configure authorization for a specific URL, for the whole site or for all sites. But the first thing to do is to create a file with a list of users and passwords. For this purpose, we will use the htpasswd utility. The syntax of the command is as follows:

$ sudo htpasswd -c /path/to/file username

In this case, the -c option is used to create a new file, you don't need to use it to edit previously created files. For example:

$ sudo htpasswd -c /etc/nginx/auth.basic admin

Basic Auth

The program will make a double password request. For security purposes, the password is not displayed, but it is entered. Once you have created such a file, you can go directly to the configuration of Nginx.

To password-protect all resources, you should add a directive to the http section of the /etc/nginx/nginx.conf file:

auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/auth.basic;

настройка Nginx

To protect a specific page address (URL), you need to add a directive to the corresponding location block. This could be /wp-admin/admin-ajax.php:

location /wp-admin/admin-ajax.php {
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/auth.basic;
}

настройка Nginx

If the site is on WordPress, it is best to place the location in location/. All the rules described above will work, plus protection is provided. If you need to allow access for a specific location , the directive will look like auth_basic "off":

location /wp-admin/admin-ajax.php {
auth_basic "off";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}

создание location

PHP processing will need to be added to the location block, otherwise the user will be prompted to download the script they are accessing.

Thus, setting up access in Nginx is not very difficult. You just need to configure the location block correctly.