0

I have the following location block in my NGINX server block:

location ~ ^/admin {
  auth_basic "Restricted";
  auth_basic_user_file /etc/nginx/.htpasswd;
  if (!-e $request_filename){
      rewrite ^(.*)$ /index.php?uri=$1;
  }
}

But it doesn't work. The /etc/nginx/.htpasswd file exists and was generated using the htpasswd command:

sudo htpasswd -c /etc/nginx/.htpasswd admin

I've done this before but it just stopped working this time. The only difference this time is that I'm trying to do auth_basic_user_file inside of a rewrite block this time.

richardgirges
  • 1,452
  • 1
  • 12
  • 17

1 Answers1

1

That config will not work because the rewrite phase, if XYZ, will always run before the access phase, auth_XYZ, regardless of how you arrange them in the location block.

See Nginx Phase Order

You need to add the authentication requirement to your php block in addition to the one you already have.

Dayo
  • 12,413
  • 5
  • 52
  • 67