Nginx how to run shell script on page load via lua module os execute and then serve directory listing?

13,304

Maybe I just figured it out? I added ngx.redirect("/"); So I have a / with location /public/html/ in my nginx.conf and also a:

    location /lua {
    default_type text/plain;
    content_by_lua '
    os.execute("/myscript.sh")
    return ngx.redirect("/")';

I would have preferred to just one 'location' instead of using redirect like that but at least I have something that seems to work. Improvements are welcome.

Share:
13,304

Related videos on Youtube

htfree
Author by

htfree

Updated on September 18, 2022

Comments

  • htfree
    htfree over 1 year

    So I have a bash script that I want to execute/run on each "page load" of url so that it will modify (creating symlinks) to a /public/html/ folder that I want the server to then serve.

    The problem is I figured out how to do one or the other not both.

    I can show the directory with nginx root /public/html/ etc. or I can run the script by having instead of root /public/html/ having:

    content_by_lua("/myscript.sh")';

    But if I use content by lua, the script does run and create what I need inside /public/html but Nginx then just shows a blank screen. I want a way to then have nginx actually show the directory listing of /public/html after it runs the myscript.sh using content_by_lua.

    I just started using nginx because of this task and am clueless how to do this, I did search around but its not an easy question to phrase for google or here.