How to run a shell script on every request?

ShellNginx

Shell Problem Overview


I want to run a shell script every time my nginx server receives any HTTP request. Any simple ways to do this?

Shell Solutions


Solution 1 - Shell

You can execute a shell script via Lua code from the nginx.conf file to achieve this. You need to have the HttpLuaModule to be able to do this.

Here's an example to do this.

location /my-website {
  content_by_lua_block {
    os.execute("/bin/myShellScript.sh")
  } 
}

Solution 2 - Shell

I found the following information online at this address: https://www.ruby-forum.com/topic/2960191

This does expect that you have fcgiwrap installed on the machine. It is really as simple as:

sudo apt-get install fcgiwrap

Example script (Must be executable)

#!/bin/sh
# -*- coding: utf-8 -*-
NAME=`"cpuinfo"`
echo "Content-type:text/html\r\n"
echo "<html><head>"
echo "<title>$NAME</title>"
echo '<meta name="description" content="'$NAME'">'
echo '<meta name="keywords" content="'$NAME'">'
echo '<meta http-equiv="Content-type"
content="text/html;charset=UTF-8">'
echo '<meta name="ROBOTS" content="noindex">'
echo "</head><body><pre>"
date
echo "\nuname -a"
uname -a
echo "\ncpuinfo"
cat /proc/cpuinfo
echo "</pre></body></html>"

Also using this as an include file, not restricted to only shell scripts.

location ~ (\.cgi|\.py|\.sh|\.pl|\.lua)$ {
    gzip off;
    root /var/www/$server_name;
    autoindex on;
    fastcgi_pass unix:/var/run/fcgiwrap.socket;
    include /etc/nginx/fastcgi_params;
    fastcgi_param DOCUMENT_ROOT /var/www/$server_name;
    fastcgi_param SCRIPT_FILENAME /var/www/$server_name$fastcgi_script_name;
}

I found it extremely helpful for what I am working on, I hope it help you out with your RaspberryPI project.

Solution 3 - Shell

  1. Install OpenResty (OpenResty is just an enhanced version of Nginx by means of addon modules ) Refer https://openresty.org/en/getting-started.html for this

  2. Configure aws cli on the instance

  3. Write a shell script which download a file from specified S3 bucket

  4. Do the required changes in nginx.conf file

  5. Restart the nginx server

I have tested the http request using curl and file gets download in /tmp directory of respective instance:

curl -I http://localhost:8080/

OutPut:

curl -I http://localhost:8080/
HTTP/1.1 200 OK
Server: openresty/1.13.6.2
Date: Tue, 14 Aug 2018 07:34:49 GMT
Content-Type: text/plain
Connection: keep-alive 
 

Content of nginx.conf file:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
           default_type text/html;
           content_by_lua '
                ngx.say("<p>hello, world</p>")
           ';
        }

        location / {
            content_by_lua_block{
            os.execute("sh /tmp/s3.sh")
            }
        }

    }
}

Solution 4 - Shell

You can also use the nginx mirror module and poxy_pass it to a web script that runs whatever, in my case I just added this to my main site location {...

mirror /mirror;
mirror_request_body off;

and then a new location called mirror that I had run a php script that executed whatever...

location = /mirror {
    internal;
    proxy_pass http://localhost/run_script.php;
    proxy_pass_request_body off;
    proxy_set_header Content-Length "";
    proxy_set_header X-Original-URI $request_uri;
}

https://nginx.org/en/docs/http/ngx_http_mirror_module.html

Solution 5 - Shell

You can use nginx's perl module which is usually part of a repo and can be easily installed. Sample to call system curl command:

   location /mint {
       perl '
            sub {
               my $r = shift;
               $r->send_http_header("text/html");
               $r->print(`curl -X POST --data \'{"method":"evm_mine"}\' localhost:7545`);
               return OK;
            }
       '; 
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaswat PadhiView Question on Stackoverflow
Solution 1 - ShellChirag Bhatia - chirag64View Answer on Stackoverflow
Solution 2 - ShellDon DuvallView Answer on Stackoverflow
Solution 3 - ShellDurgesh PandeyView Answer on Stackoverflow
Solution 4 - ShellkierzoView Answer on Stackoverflow
Solution 5 - ShellAndrei .FView Answer on Stackoverflow