如何从Nginx运行bash脚本 [英] How to run bash script from Nginx

查看:61
本文介绍了如何从Nginx运行bash脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)我有静态站点,并且可以通过命令从位桶中设置自动拉动".

1) I have static site and wand to set "autopull" from bitbucket.

2)我有来自bitbucket的webhook.

2) I have webhook from bitbucket.

3)我有执行"git pull"的bash脚本

3) I have bash script which do "git pull"

当nginx catch请求时如何运行此脚本?

How can I run this script when nginx catch request?

server {

    listen   80;
    server_name example.ru;

    root /path/to/root;
    index index.html;

    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;

    location /autopull {
        something to run autopull.sh;
    }

    location / {
        auth_basic "Hello, login please";
        auth_basic_user_file /path/to/htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;
    }
}

我尝试了lua_block和fastcgi服务,但是都失败了.lua不运行os.execute("/path/to/script")并且不写入日志.fastcgi更成功,但没有权限,因为我的www-data用户在我的位批处理存储库中没有ssh-key.

I tried lua_block and fastcgi service, but both are failed. lua does not run os.execute("/path/to/script") and does not write the log. fastcgi is more successful, but it has not permissions, because my www-data user doesn't have ssh-key in my bitbuchet repo.

推荐答案

问题已解决.

我不想在另一个端口上使用任何脚本/进程,因为我有多个站点,每个站点都需要一个端口.

I didnt want to use any script/process on another port because I have several sites and I need port for each.

我的最终配置是:

server {

    listen   80;
    server_name example.ru;

    root /path/to/project;
    index index.html;

    access_log /path/to/logs/nginx-access.log;
    error_log /path/to/logs/nginx-error.log;

    location /autopull {
        content_by_lua_block {
            io.popen("bash /path/to/autopull.sh")
        }
    }

    location / {
        auth_basic "Hello, login please";
        auth_basic_user_file /path/to/htpasswd;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;
    }
}

问题已在www-data用户及其回购协议中的ssh-kay允许.

Problem was in permission of www-data user and its ssh-kay in repo.

这篇关于如何从Nginx运行bash脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆