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 ?

poka
  • 135
  • 3
  • 13
  • You cannot test the query string in the `location` directive. – Richard Smith Sep 04 '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 04 '17 at 18:08

1 Answers1

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.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • 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 05 '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 05 '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 06 '17 at 01:16
  • I'm surprised there no simpler solution... – poka Sep 06 '17 at 01:20
  • What exactly happened when "it didn't work"? And did you reload nginx configuration after the change? – Tero Kilkanen Sep 06 '17 at 10:15
  • Nothing happend, like I never touch the configuration. Of course I reloaded nginx – poka Sep 06 '17 at 11:13
  • "Nothing happened" tells nothing useful. What exactly did you do, and what was the exact thing that happened, and what exactly did you expect to happen? – Tero Kilkanen Sep 06 '17 at 11:25
  • Ok sorry. As I explained in my topic, the page `https://exemple.com/doku.php?id=staff`should ask a htaccess password to be reach. So as you suggested, I added the map directive in my nginx.conf file under http directive, and in my file.conf under conf.d folder, I specify `auth_basic $authrequired;` in 'location /doku.php' directive, as I wrote it ... But when I go to `https://exemple.com/doku.php?id=staff`, this page doesn't ask any password. But this page should ask a password, it's the objectif of this manipulation. – poka Sep 06 '17 at 19:55
  • Nobody can help me here ? – poka Sep 23 '17 at 14:34