通过nginx代理请求执行shell脚本 带参数

后台生成HTML页面上传到oss上,由于oss运营商禁止了直接访问HTML页面,所以要把HTML下载到两台nginx服务器下面,当生成上传之后再通过nginx接口执行shell同步文件!!!

1. 安装openresty

参考:http://openresty.org/cn/installation.html

2. 安装sockproc

git clone https://github.com/juce/sockproc
cd sockproc
make
./sockproc /data/tomcattemp/shell.sock
chmod 666 /data/tomcattemp/shell.sock

3. 安装lua-resty-shell模块

git clone https://github.com/juce/lua-resty-shell
cd lua-resty-shell
cp lib/resty/shell.lua /usr/local/openresty/lualib/resty 

其中/usr/local/openresty/是安装openresty的目录

4. 在usr/local/openresty/lualib下创建lua脚本

vim ps.lua

local uri = ngx.var.uri;
local args = ngx.req.get_uri_args();
local name  = args["name"];
local shellCommand ="ps -ef|grep "..name
local shell = require "resty.shell"
local args = {
    socket = "unix:/data/tomcattemp/shell.sock";
}
local status, out, err = shell.execute(shellCommand, args)
ngx.header.content_type = "text/plain"
ngx.say("Result:"..shellCommand.."\n" .. out)

5. 增加NGINX访问入口

vim /usr/local/openresty/nginx/conf/nginx.conf

在server模块添加

location = /api/ps {
    content_by_lua_file /usr/local/openresty/lualib/ps.lua;
}

保存后重启nginx,/usr/local/openresty/nginx/sbin/nginx -s reload

6. 测试效果

浏览器打开:http://ip:80/api/ps?name=java

就可以看到ps -ef|grep java的命令行结果了

# nginx   shell  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×