2

I need to run a script from nginx (preferably PHP) before every request. All my served pages are plain HTML. Googling took me as far as LUA but, even though I have full access to server I would rather not change anything apart from nginx configuration (either http or server blocks).

I have php5-fpm also running on-site.

The reason is, we would like to allow developers do create HTML static one-page-apps that would need to AJAX post to an external API. CORS and all setup fine, all good, but we wanted to initiate a session on the destination API server.

5
  • What do you want the script to change? – Michael Hampton Mar 3 '16 at 23:11
  • I'll use the script to start a session on the destinaiton API server (basically cURL to it) – eduardev Mar 3 '16 at 23:15
  • Yeah, the Lua module can handle that just fine. Of course you know those are scripts embedded inside your nginx configuration files. – Michael Hampton Mar 3 '16 at 23:15
  • yes, I'm starting to get frustrated already with googling for an alternative to Lua. Probably that's the only way!? – eduardev Mar 3 '16 at 23:17
  • You could abuse auth_request for this – Alexey Ten Mar 4 '16 at 7:10
0

To whom it may interest, I ended up using the following solution (Thanks to all the comments that helped me follow along):

Hope this can help someone.

Note: As alerted in comments, this has a significant impact on performance, because nginx will not serve static files anymore as it will need to send them back and forth to PHP-FPM.

4
  • 1
    Be aware that this has a significant performance penalty, from having to process static files with a script interpreter, and so it is unlikely to scale very well. – Michael Hampton Mar 12 '16 at 21:36
  • @MichaelHampton Does it perform worst compared to processing a normal script file like php ? – eduardev Mar 12 '16 at 21:47
  • 1
    It probably won't be too bad, as long as there isn't actually any PHP code in the file. In that case it just has to read it and spit it back out at the browser. But that's still much slower than simply having nginx send the file with the kernel-accelerated sendfile() call, which is what it normally does when serving a static file. – Michael Hampton Mar 12 '16 at 21:48
  • @MichaelHampton right, ok then. Avoiding PHP code inside our files was a priority, of course performance too. So I guess this is still is good solution if I can't recompile nginx with lua. Thanks for the comment Michael. – eduardev Mar 12 '16 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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