网站首页 > 技术教程 正文
图/文:迷神
让Nginx使用url访问的模式,也可以执行linux的shell命令是一件很爽的事情。本文就是使用小巧的lua脚本,Nginx我使用春哥的 openresty,当然如果你自己使用原版nginx,那需要编译下lua环境。
如果大家怕麻烦,可以使用的宝塔安装的openresty环境。
一、我们先安装:sockproc
sockproc 是一个服务器程序, 侦测socket ,unix 或者 tcp , 并把收到的命令,传递给子进程执行,执行完毕后,把结果返回给客户端
git clone https://github.com/juce/sockproc #git克隆代码
cd sockproc
make #编译
./sockproc /tmp/cmd.sock
chmod 0666 /tmp/cmd.sock
二、下载lua-resty-shell模块
一个很小巧的非阻塞的shell执行库,用来配合openresty 使用,具体大家可以去github上看他使用的的demo
git clone https://github.com/juce/lua-resty-shell
cd lua-resty-shell
cp lib/resty/shell.lua /openresty/lualib/resty/ #这是你的项目路径
创建lua脚本
vi /openresty/lualib/cmd.lua --创建文件command.lua,输入下面代码
local shell = require "resty.shell"
local args = {
socket = "unix:/tmp/cmd.sock", --这是第一步的unxi socket
}
local status, out, err = shell.execute("ls", args) --ls 是想调用的命令,
ngx.header.content_type = "text/plain"
ngx.say("Result:\n" .. out) -- 命令输出结果
三、写入nginx配置
vi /openresty/nginx/conf/nginx.conf
#增加一个localtion 配置
location = /api/ls {
content_by_lua_file /southtv/openresty/lualib/cmd.lua;
}
然后重启下openresty,通过http://你的IP/api/ls 就可以访问啦。
猜你喜欢
- 2024-10-09 openresty代替nginx并使用lua扩展功能
- 2024-10-09 Redis弱事务性与Lua脚本原子性分析
- 2024-09-08 基于Nginx+lua的蓝绿发布系统(lvs与nginx)
- 2024-09-08 Go Web 框架 Gin 实践17—用 Nginx 部署 Go 应用
- 2024-09-08 一文看懂灰度发布——基于Nginx+Lua+Redis
- 2024-09-08 Lua 基础入门(lua经典详细入门教程中文pdf)
- 2024-09-08 Redis中使用Lua脚本来实现并发下的原子操作
- 2024-09-08 运维篇—基于Nginx+Lua实现的灰度发布
- 2024-09-08 Nginx+Lua+Redis实现高性能缓存数据读取
- 2024-09-08 OpenResty实战-Lua入门-Lua模块(openresty教程)
你 发表评论:
欢迎- 最近发表
-
- Win11学院:如何在Windows 11上使用WSL安装Ubuntu
- linux移植(Linux移植freemodbus)
- 独家解读:Win10预览版9879为何无法识别硬盘
- 基于Linux系统的本地Yum源搭建与配置(ISO方式、RPM方式)
- Docker镜像瘦身(docker 减小镜像大小)
- 在linux上安装ollama(linux安装locale)
- 渗透测试系统Kali推出Docker镜像(kali linux渗透测试技术详解pdf)
- Linux环境中部署Harbor私有镜像仓库
- linux之间传文件命令之Rsync傻瓜式教程
- 解决ollama在linux中安装或升级时,通过国内镜像缩短安装时长
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)