nginx - How to run a shell script on every request?

后端 未结 4 538
一向
一向 2020-12-04 13:18

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

相关标签:
4条回答
  • 2020-12-04 13:34

    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")
      } 
    }
    
    0 讨论(0)
  • 2020-12-04 13:40

    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.

    0 讨论(0)
  • 2020-12-04 13:42
    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")
                }
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-04 13:43

    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

    0 讨论(0)
提交回复
热议问题