0

I'm trying to put an htpasswd only for a group of sub-pages under this url:

https://exemple.com/doku.php?id=staff

So for instance this page should be include:

https://exemple.com/doku.php?id=staff:modo

I tried to do this in nginx:

location ~ ^/doku.php?id=staff(.*) {

   auth_basic "Staff only";
   auth_basic_user_file /etc/nginx/.htpasswd/.htpasswd-staff;
}

But there no action, even on the main page exemple.com/doku.php?id=staff

I tried also this:

location ~ ^/doku.php(.*) {

  if ( $query_string = "id=staff" ) {
    auth_basic "Staff only";
    auth_basic_user_file /etc/nginx/.htpasswd/.htpasswd-staff;
  }
}

But can't reload nginx with error:

nginx: [emerg] "auth_basic" directive is not allowed here in /etc/nginx/conf.d/site.conf:31

What did I miss ?

  • You cannot test the query string in the location directive. – Richard Smith Sep 4 '17 at 15:24
  • I tried to make this: location ~ ^/doku.php(.*) { if ( $query_string = "id=staff" ) { auth_basic "Staff only"; auth_basic_user_file /etc/nginx/.htpasswd/.htpasswd-staff; } } But can't reload nginx with error: nginx: [emerg] "auth_basic" directive is not allowed here in /etc/nginx/conf.d/site.conf:31 – poka Sep 4 '17 at 18:08
0

I haven't tested this myself, but I think this can be implemented with the map directive:

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;
    }
}

The map directive makes nginx assign different values to $authrequired variable depending on the value of query argument id. Default value is off, and when id contains "staff", $authrequired becomes "Staff only".

Then we use this variable for auth_basic directive.

| improve this answer | |
  • I can't use the http directive anywhere, even outside of server directive: nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/wiki.conf:1 Same thing for map directive in server... – poka Sep 5 '17 at 13:40
  • You need to include the map inside your existing http directive. Depending on your distribution, you may need to put it in your nginx.conf directly or if there is a file included in the http level, then you can put the map there. – Tero Kilkanen Sep 5 '17 at 14:44
  • Ok so I tired it, and it didn't work ... I tried alos with id=staff "Staff only"; in map directive, but nothing happend – poka Sep 6 '17 at 1:16
  • I'm surprised there no simpler solution... – poka Sep 6 '17 at 1:20
  • What exactly happened when "it didn't work"? And did you reload nginx configuration after the change? – Tero Kilkanen Sep 6 '17 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.