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(/|$) {
...
}
}You can use regex expressions with the map directive using ~ modifier, for example:
map $host $realm {
~appname.example.com$ "Resticted content";
default off;
}Reference: https://stackoverflow.com/a/69538528
