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

后端 未结 4 552
一向
一向 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: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("

    hello, world

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

提交回复
热议问题