使用shell做http web接口,可以传递参数
参考网址:
https://me.jinchuang.org/archives/114.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 < /data/shell/v1/api/demo <window.setInterval(function(){
# window.location.reload();
#},1000);'
#echo ''
# html页面css样式
#echo ''
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错误。