Custom Plugin execution order

Is there any way to control plugin execution order? e.g. If I want to run custom plugin xyz, right after my JWT plugin executes or right before rate-limiting plugin. How can I achieve it?

Also, is there any way to inject any script() or code in existing kong plugin to perform some additional stuff. e.g. in JWT plugin apart from authentication I want to do some other checks too.

The plugin priorities are currently hard-coded:

We do want to change these to make them dynamic so that you can more easily define how plugins should be executed.

No, this is not possible. You can clone the plugin and create a custom plugin based on the what we ship in OSS.

Thank you for the clarification.
I have follow up questions on related topic.

  1. Where can I find hard coded plugin execution order?
  2. Which plugin does route matching for a incoming request?

In the above doc that I linked. :slight_smile:

That happens in the core logic of Kong. Route matching happens before a pluign is executed in the access phase.

It is really helpful to get right information. Thanks for the prompt reply.

So, route matching is not implemented in any of plugins? route matching happens before any plugin is executed?

My use case is, add some custom logic, right after the route is matched.
Is there any suggestion to utilize existing route matching functionality from kong and add some additional custom stuff once route is matched? ?

Correct. There are exceptions to this but those are rare and not recommended.

Add a plugin with higher priority than zipkin. That should ensure that your plugin is executed after route matching is performed.

Thanks. This will give current matched route. Thus route matching can be mimicked in custom plugin by using this. Once matching route is found, any additional operations can be performed. No need to maintain the plugin execution order.

Is my understanding correct?


Can you explain in detail what is it that you are trying to do?

Use-case: Get matched route and perform some operations on matched route.

I was thinking of two approaches.

  1. Override native plugin of route matching and add additional functionality apart from native logic.
    Now, route matching functionality is performed in the core logic and not in plugins. So, this won’t work.

  2. Introduce a custom plugin, somehow retrieve the matched route, perform additional functionality.

As per,, I can retrieved current matched route in custom plugin and add required additional operations there.

Plugin Execution does required in 2nd approach.

Can you elaborate on what you mean by “some operation”?

Add custom authorization. Add some metrics.

You don’t need to hook into router for that purpose.
The router is used for changing the route that was matched.
In your case, you need to define a route and attach a plugin to that route. The plugin will be executed only when there is a request that matches the route you had defined.

I use “kong.response.exit(res.status, res.body)”
when res.status=200,The interface is open ,no problem
but when res.status!=200,have questions
questions is No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

Did I configure it right?
thank you~

Can you please initiate different thread for your question?