0

As the question implies I want to allow full access to my website if the X-Auth HTTP header is set and contains a specific string. If this is not the case, HTTP basic authentication should be triggered. Something like this:

if ($http_x_auth = "some_string") {
  allow access;
} else {
  auth_basic "Authentication";
  auth_basic_user_file /etc/nginx/security_http_basic_auth_file;
}

Unfortunately NGINX does not allow if else statements. Does anyone have an idea how this could be done?

manifestor
  • 6,079
  • 7
  • 27
  • 39

1 Answers1

0

You could probably do it with map.

In the http block:

map $http_x_auth $has_basic_auth {
    "some_string" no_basic_auth;
    default with_basic_auth;
}

In your location block:

try_files nonexistent @$has_basic_auth;

And then add two named locations:

location @no_basic_auth {
    # insert your config
}
location @with_basic_auth {
    # insert your config with basic auth
}
anx
  • 8,963
  • 5
  • 24
  • 48
ampularius
  • 101
  • 3