Require password with nginx

Nginx is a web server, we can also use it as a reverse proxy, and we can simply add basic authentication to the server.

Nginx is a web server, we can also use it as a reverse proxy, and we can simply add basic authentication to the server.

Install htpasswd tool

In order to use the htpasswd command line tool, we need install it first, on CentOS 8, we can use the following command to install it:

dnf install -y httpd-tools

Generate password

# Generate pasword with htpasswd tool
htpasswd /etc/nginx/.htpasswd user
# Input the password here

Add configuration

location /api { 
    auth_basic           "Password Notification"; 
    auth_basic_user_file /etc/nginx/.htpasswd;  
}

References

Restricting Access with HTTP Basic Authentication