我试图把一个htpasswd只为这个url下的一组子页面:
https://exemple.com/doku.php?id=staff
所以比如这个页面应该包括:
https://exemple.com/doku.php?id=staff:modo
我试图在nginx中做到这一点:
location ~ ^/doku.php?id=staff(.*) { auth_basic "Staff only"; auth_basic_user_file /etc/nginx/.htpasswd/.htpasswd-staff; }
但是,即使在主页面exemple.com/doku.php?id=staff上也没有任何操作
我也试过这个:
location ~ ^/doku.php(.*) { if ( $query_string = "id=staff" ) { auth_basic "Staff only"; auth_basic_user_file /etc/nginx/.htpasswd/.htpasswd-staff; } }
但不能重新加载nginx的错误:
nginx: [emerg] "auth_basic" directive is not allowed here in /etc/nginx/conf.d/site.conf:31
我错过了什么 ?
我自己没有testing过,但我认为这可以通过map指令来实现:
http { map $arg_id $authrequired { default off; staff "Staff only"; } } server { location /doku.php { auth_basic $authrequired; auth_basic_user_file /etc/nginx/.htpasswd/.htpasswd-staff; } }
map指令使得nginx根据查询参数id的值将不同的值赋给$authrequiredvariables。 默认值是closures的,当id包含“staff”时, $authrequired变成“Staff only”。
然后我们使用这个variables作为auth_basic指令。