Ubuntu 24.04
Sponsored Link

Nginx : Basic Authentication2024/05/31


Set Basic Authentication to limit access on specific web pages.


Username and password are sent with plain text on Basic Authentication,
so Use secure connection with SSL/TLS setting, refer to here.

[2] Add setting on a site config you'd like to set.
For example, set Basic Authentication under the [/auth-basic] directory.
apt -y install apache2-utils
vi /etc/nginx/sites-available/default
# add into the [server] section

server {
        location /auth-basic {
              auth_basic            "Basic Auth";
              auth_basic_user_file  "/etc/nginx/.htpasswd";

mkdir /var/www/html/auth-basic

systemctl reload nginx
# add users for basic authentication

htpasswd -Bc /etc/nginx/.htpasswd ubuntu

New password:    
# set any password

Re-type new password:
Adding password for user ubuntu
# create a test page

vi /var/www/html/auth-basic/index.html
<p style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">
Test Page for Basic Authentication
[3] Access to the test page from any client computer with web browser. Then authentication is required as settings, answer with a user added in [2].
[4] That's OK if authentication is successfully passed and test page is displayed normally.
Matched Content