使用shell做http web接口,可以传递参数
作者:互联网
镜像:
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脚本了
注意:在该目录下存放的shell脚本需要有x可执行权限,否则会报403错误。
优化,使用现成的linux-shell模板
标签:web,shell,http,fcgiwrap,nginx,entrypoint,docker,data 来源: https://www.cnblogs.com/sanduzxcvbnm/p/15576779.html