使用shell做http web接口,可以传递参数--废弃

此文章废弃,参考另一篇

参考网址:
https://me.jinchuang.org/archives/114.html
https://www.cnblogs.com/jinchuang/p/14200587.html
https://hub.docker.com/r/ipyker/fcgiwrap-nginx-shell
https://github.com/ipyker/fcgiwrap-nginx-shell/blob/main/Dockerfile
https://github.com/ipyker/fcgiwrap-nginx-shell/blob/main/docker-entrypoint.sh

镜像:

docker pull ipyker/fcgiwrap-nginx-shell

启动:

docker run -d --name nginx-fcgiwrap -p 80:80 ipyker/fcgiwrap-nginx-shell

使用:

curl http://127.0.0.1/v1/api/demo   

curl "http://127.0.0.1/v1/api/demo?abc&efg"

Dockerfile

FROM nginx:1.17.9

RUN apt-get update && apt-get install -y spawn-fcgi fcgiwrap \
 && apt-get clean \
 && rm -rf /var/lib/apt/lists/*

EXPOSE 80

STOPSIGNAL SIGTERM

COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]

docker-entrypoint.sh

#!/bin/bash
# ----------------------------------------------------------------
# Filename:       docker-entrypoint.sh
# Revision:       1.1
# Date:           2021-08-26
# Author:         pyker.zhang
# Email:          pyker@qq.com
# website:        www.ipyker.com
# Description:    使用shell写http web接口
# ----------------------------------------------------------------

# nginx支持fcgiwrap配置
cat > /etc/nginx/conf.d/default.conf <<EOF
server {
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ ^/v1/api/(.*)$ {
        gzip off;
        default_type  text/plain;
        root   /data/shell;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        include fastcgi_params;
    }
}
EOF

# 创建shell脚本目录
mkdir -p /data/shell/v1/api

# 创建一个demo脚本
cat > /data/shell/v1/api/demo <<EOF
#!/bin/sh
echo "Content-Type:text/html;charset=utf-8"
echo ""
# 自动刷新
#echo '<script>window.setInterval(function(){
#    window.location.reload();
#},1000);</script>'
#echo '<meta http-equiv="refresh" content="60">'
# html页面css样式
#echo '<style>
#body{color:#cecece;}
#.title{color: #FF9800;border-left: 4px solid;padding: 4px;}
#pre{font-size:14px;border-left: 4px solid #4CAF50;padding: 5px;}
#</style>'
for i in a b c; do
	echo \$i
done
# Passing parameters
echo "\$QUERY_STRING" | awk -F '&' '{print \$1}'
echo "\$QUERY_STRING" | awk -F '&' '{print \$2}'
EOF

chmod +x /data/shell/v1/api/demo
/etc/init.d/fcgiwrap start
chmod 766 /var/run/fcgiwrap.socket
nginx -g "daemon off;"

进一步使用

本机创建挂载目录,然后启动容器的时候指定该路径进行挂载

mkdir -p /data/shell
docker run -d --name nginx-fcgiwrap -p 80:80 -v /data/shell:/data/shell/ ipyker/fcgiwrap-nginx-shell

此时在本机的/data/shell/v1/api路径下写shell脚本,然后访问请求,就能执行该shell脚本了

本地挂载路径要跟容器中nginx配置文件中写的路径一致,否则会报403 错误

注意:在该目录下存放的shell脚本需要有x可执行权限,否则会报403错误。

posted @   哈喽哈喽111111  阅读(696)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2020-11-19 《经乱离后天恩流夜郎忆旧游书怀赠江夏韦太守良宰》
2020-11-19 使用scrapy爬取长安有妖气小说
2016-11-19 mysql启用慢日志查询
2016-11-19 linux下mysql忘记root密码解决方法
2016-11-19 记录php日志
点击右上角即可分享
微信分享提示