如何从Nginx运行bash脚本
1) 我有静态网站和魔杖设置“自动拉”从bitbucket 2) 我有bitbucket的webhook 3) 我有一个bash脚本,它执行“git pull” 当nginx捕获请求时,如何运行此脚本如何从Nginx运行bash脚本,bash,git,nginx,Bash,Git,Nginx,1) 我有静态网站和魔杖设置“自动拉”从bitbucket 2) 我有bitbucket的webhook 3) 我有一个bash脚本,它执行“git pull” 当nginx捕获请求时,如何运行此脚本 server { listen 80; server_name example.ru; root /path/to/root; index index.html; access_log /path/to/logs/nginx-access.log;
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数据用户在我的bitbuchet repo中没有ssh密钥。基于,创建py脚本
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from subprocess import call
PORT_NUMBER = 8080
autopull = '/path/to/autopull.sh'
command = [autopull]
#This class will handles any incoming request from
#the browser
class myHandler(BaseHTTPRequestHandler):
#Handler for the GET requests
def do_GET(self):
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
self.wfile.write("runing {}".format(autopull))
call(command)
return
try:
#Create a web server and define the handler to manage the
#incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print 'Started httpserver on port ' , PORT_NUMBER
#Wait forever for incoming htto requests
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
server.socket.close()
运行它并在nginx配置中添加
location /autopull { proxy_pass http://localhost:8080; }
问题解决了
我不想在另一个端口上使用任何脚本/进程,因为我有几个站点,每个站点都需要端口
我的最终配置是:
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数据用户及其ssh kay在repo中的权限 为什么不能通过cron运行这个脚本并让脚本执行类似s3同步的操作?这样,只有在发生更改时,才会拉取文件