Jacobian
Jacobian

Reputation: 10802

Nginx location block that matches a group of urls

I have an IdentityAccess controller in my application, that contains four action methods:

  1. login
  2. check
  3. refresh
  4. revoke

So, I can access these action methods by localhost/IdentityAccess/login and etc. Besides, I implemented Lua validation, that should secure some of the routes. For example, this is how I secure all requests coming to store controller:

location /store/ {
    rewrite_by_lua_file 'jwt-validation.lua';
    proxy_pass http://store_server/
    ...
}

But the whole problem is with IdentityAccess controller. In this controller I want to secure all action methods except login. Besides, I do not want to create a whole bunch of exact locations like location /IdentityAccess/check { ..., location /IdentityAccess/refresh { ... and etc. and apply to each location Lua script. What I want is some kind of regex that would match a list of action methods - check, refresh and revoke - and would ignore login action method. But I do not know what is the best way to achieve this.

Upvotes: 3

Views: 505

Answers (1)

Dmitry MiksIr
Dmitry MiksIr

Reputation: 4445

Prefix location with the longest matching prefix is selected. So all you need is define /IdentityAction/login/ location without jwt validation.

location /IdentityAction/ {
    rewrite_by_lua_file 'jwt-validation.lua';
    proxy_pass http://store_server;
    ...
}

location /IdentityAction/login/ {
    proxy_pass http://store_server;
    ...
}

Sure, this can be done with regexp location too, but in this case position of location in file is important, because first match selected (but for prefix locations position is not important because best match of location selected). With regexp it can look like

location ~ ^/IdentityAction/login/$ {
    # first location in file
    proxy_pass http://store_server;
}

location ~ ^/IdentityAction/$ {
    # second location in file
    rewrite_by_lua_file 'jwt-validation.lua';
    proxy_pass http://store_server;
}

Upvotes: 2

Related Questions