ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

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

2021-11-19 13:31:52  阅读:203  来源: 互联网

标签:web shell http fcgiwrap nginx entrypoint docker data


镜像:

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有