I want to authorize the user based on his email without it getting super messy. Is there a better way to do this?
http {
map $http_user $user {
default $http_user;
}
server {
...
location ~ ^(/deployments/).*(xxx).* {
set $allow 0;
if($user = "[email protected]" || $user = "[email protected]" || $user = "[email protected]"){
set $allow 1;
}
if($allow = 0){
return 403 {error: you are not authorized}
}
}
location ~ ^(/deployments/).*(yyy).* {
set $allow 0;
if($user = "[email protected]" || $user = "[email protected]" || $user = "[email protected]"){
set $allow 1;
}
if($allow = 0){
return 403 {error: you are not authorized}
}
}
}
}
Is there a better way to do this?
I tried the above code but it doesn’t work
2
Answers
You code will not work because of the way
if
functions when inside a location.You can use a
map
instead, which can be cascaded from your existingmap
.For example:
Expressions containing brace characters must be quoted, also JSON strings should be quoted.
If you have ngx_http_auth_request_module installed, you can leverage
auth_request
directive to avoidif
block in eachlocation
: