Configuring Nginx for basic authentication

To configure Nginx for basic authentication, you need to create a file for user and password, update Nginx configurations, and restart Nginx.

  1. Create an user-password file for Nginx.
    htpasswd -c /etc/nginx/.htpasswd admin

    This requires the Apache HTTP package to be installed on the system.

  2. Update your Nginx configuration (/etc/nginx/nginx.conf or a custom configuration file in the /etc/nginx/conf.d directory) with the highlighted portion in the code below:
        server {
            listen              9443 ssl;
            server_name         _;
            ssl_certificate     /<PATH TO CERTIFICATE>/nginx-server-crt.pem;
            ssl_certificate_key /<PATH TO CERTIFICATE>/nginx-server-key.pem;
    
            location /prometheus/ {
                auth_basic           "Prometheus";
                auth_basic_user_file /etc/nginx/.htpasswd;
                proxy_pass http://localhost:9090/;
            }
        }
  3. Restart Nginx.