I have an nginx server running on ubuntu server. I have many websites and hundreds of pages hosted on the server. I want to execute one PHP function on each page when it is requested by a user. It is not feasible to add/call that function on all the websites and each & every page manually.

Is there a way in nginx where I can define which PHP file/function it should execute before serving the request to a user?

NGINX does not have this capacity as part of stock NGINX functions. This is something that needs to happen at the PHP level - which is not nginx - if it has to run for every request it has to be done at PHP or the PHP application.

You may be able to do this with some type of Lua code done by the third-party Lua module (which must be manually compiled in, unless you use nginx-extras) that passes a request before processing to PHP backend but that will be executed separately from the sites and their web app items, quite possibly earlier than you want it to (I am not an expert in the Lua module and its functionality though).

This is NOT however a typical use case so such preprocessing of requests is not part of the design spec for nginx and possibly not easy to accomplish via the Lua module either.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.