How to register multiple middleware in Actix web

I am trying to register multiple actix web middleware like this

                .wrap(Middleware1 {})
                .wrap(Middleware2 {})


But I realise that Middleware1 is never executed on requests, but Middleware2 is. But when I remove Middleware2, then Middleware1 executes.

So it seems somehow only the last middleware is registered. This is obviously not want I want.

So the question is, how do I register multiple middlewares correctly?


  • As per the documentation

    Warning: if you use wrap() or wrap_fn() multiple times, the last occurrence will be executed first.

    Based on that, it is likely both of your middleware are executed, not in the order you would expect. If the second middleware somehow rely on the result of the first one, then you need to change their order.

    You can try with middlewares that simply log something to the console to see their order of execution.