NGINX Conditional Basic Auth

You can use map block to get an auth_basic directive argument from the $host variable value:

map $host $realm {
    appname.example.com  "Resticted content";
    default              off;
}

server {
    root   /var/www/html/public;
    index index.php;

    auth_basic $realm;
    auth_basic_user_file /var/www/html/public/.htpasswd;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ [^/]\.php(/|$) {
        ...
    }
}Code language: Nginx (nginx)

You can use regex expressions with the map directive using ~ modifier, for example:

map $host $realm {
    ~appname\.example\.com$  "Resticted content";
    default                  off;
}Code language: Nginx (nginx)

Reference: https://stackoverflow.com/a/69538528


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.