1、cig:公共网关接口
2、工具
- Centos 7
- epel-release // 需要先安装这个源
- curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
- yum install -y nginx fcgiwrap
- Ubantu 18:
- apt install -y nginx fcgiwrap // -y表示同意 fcgiwrap是cgi实现
3、fcgiwrap配置
- 方式一:nohup fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket & :nginx和web服务必须是一台电脑
- 方式二:nohup fcgiwrap -f -c 4 -s tcp:0.0.0.0:9000 & :两台电脑
- 帮助:fcgiwrap -h; man fcgiwrap
- -f:标准错误输出
- -c:启动进程数
- -s:指定的socket文件放到哪里 socket文件一般放在unix/run目录下
- nohup &:表示后台运行程序
[root@moli ~]# fcgiwrap -h
Usage: fcgiwrap [OPTION]
Invokes CGI scripts as FCGI.
fcgiwrap version 1.1.0
Options are:
-f Send CGI's stderr over FastCGI
-c <number> Number of processes to prefork
-s <socket_url> Socket to bind to (say -s help for help)
-h Show this help message and exit
-p <path> Restrict execution to this script. (repeated options will be merged)
Report bugs to Grzegorz Nosek <root@localdomain.pl>.
fcgiwrap home page: <http://nginx.localdomain.pl/wiki/FcgiWrap>
[root@moli ~]# ls
hosts influxdb influxdb.tar java jmeter mariable mariadb mariadb2 programs testlink tmp tomcat
[root@moli ~]# mkdir fcgiwrap
[root@moli ~]# ls
fcgiwrap hosts influxdb influxdb.tar java jmeter mariable mariadb mariadb2 programs testlink tmp tomcat
[root@moli ~]# cd fcgiwrap/
[root@moli fcgiwrap]# ls
[root@moli fcgiwrap]# nohup fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket &
[1] 197239
[root@moli fcgiwrap]# nohup: ignoring input and appending output to 'nohup.out'
[root@moli fcgiwrap]# ls
nohup.out
[root@moli fcgiwrap]# cat nohup.out
[root@moli fcgiwrap]# ps -ef | grep cgi
root 197239 196425 0 13:13 pts/0 00:00:00 fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket
root 197240 197239 0 13:13 pts/0 00:00:00 fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket
root 197241 197239 0 13:13 pts/0 00:00:00 fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket
root 197242 197239 0 13:13 pts/0 00:00:00 fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket
root 197243 197239 0 13:13 pts/0 00:00:00 fcgiwrap -f -c 4 -s unix:/run/fcgiwrap.socket
root 197346 196425 0 13:14 pts/0 00:00:00 grep --color=auto cgi
4、nginx配置
- 新建目录:mkdir -p /data/nginx/test_cgi/cgi/ // cgi目录必须有 否则会报错
- /etc/nginx/conf.d/test_cgi.conf // nginx配置信息目录
- chown nginx:nginx /run/fcgiwrap.socket
- 参考:fastcgi_param
[root@moli conf.d]# cat test_cgi.conf
server {
listen 8888; // 监听的端口
server_name moli.press; // 配置的域名 没有域名的时候使用_
root /data/nginx/test_cgi/; // 使用nginx时候 nginx的配置文件信息目录
error_log /data/nginx/test_cgi/error.log; // 错误日志
access_log /data/nginx/test_cgi/access.log; // 正常访问日志
location /cgi/ {
fastcgi_pass unix:/run/fcgiwrap.socket; // 访问nginx时,使用fastcgi_pass找到fcgiwrapper,和fcgiwrapper沟通执行
include fastcgi_params; // 加载fastcgi的变量
fastcgi_index index.cgi; // 默认打开index.cgi文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; // nginx传过来的信息,需要执行一些脚本,把一写脚本信息传过来
}
}
// 访问:
www.XXXX:8888/cgi/
--- 报错502
[root@moli nginx]# cat nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
// nginx的默认用户时nginx 下面的文件默认时root用户 所以默认不会访问 fcgiwrap.socket
[root@moli /]# ls -l /run/fcgiwrap.socket
srwxr-xr-x 1 root root 0 Nov 22 13:13 /run/fcgiwrap.socket
// 修改文件的用户
[root@moli /]# chown nginx:nginx /run/fcgiwrap.socket
[root@moli /]# ls -l /run/fcgiwrap.socket
srwxr-xr-x 1 nginx nginx 0 Nov 22 13:13 /run/fcgiwrap.socket
// 然后呢?报错403
// 因为这个目录下 没有内容
// data/nginx/test_cgi/cgi // 编写脚本存放处
[root@moli cgi]# pwd
/data/nginx/test_cgi/cgi
[root@moli cgi]# ls
[root@moli cgi]# vim index.cgi
[root@moli cgi]# cat index.cgi
echo -e 'Content-Type: text/html;charset=utf-8\n\n'
echo 'nginx'
[root@moli cgi]# chmod +x index.cgi
[root@moli cgi]# ls
index.cgi
// 喵的 又是502错误 Bad Gateway
// 因为上面的index.cgi脚本有问题 访问页面是get post请求 index.cgi内容清空了
[root@moli cgi]# vim cgi.sh
[root@moli cgi]# cat cgi.sh
#! /bin/bash
# // 定义注释
html(){ // 定义一个函数
echo -e "Content-Type: text/html;charSet=utf-8\n\n"
}
get(){
html // 指定函数
echo "get 方法" // 打印
if [[ -n $QUERY_STRING ]]; then // 判断是get
echo $QUERY_STRING
fi
}
post(){
html
echo "post 方法"
if [[ -n $POST_DATA ]]; then // 判断是post
echo $POST_DATA
fi
}
cgi(){
if [ "$REQUEST_METHOD" = "GET" ];then // 请求方式是get的话
get "$REQUEST_METHED"
elif [ "$REQUEST_METHOD" = "POST" ];then
read -n $CONTENT_LENGTH POST_DATA
post "$POST_DATA"
fi
}
cgi
// 好了 哈哈哈
5、curl测试 nginx
- get请求:curl “http://www.XXX:8888/cgi/index.cgi?a=1&b=2”
- 或者:http://www.moli.press:8888/cgi/?a=1&b=2
- post请求: -X指定post请求
- urlencoded格式:curl “http://www.XXX.press:8888/cgi/” -X POST -d ‘user_id=123&msg=“OK!”’
- json格式:curl “http://www.XXX:8888/cgi/”-H “Content-Type:application/json” -X POST -d ‘{“user_id”:“123”,“msg”:“OK!”}’
[root@moli cgi]# vim index.cgi
[root@moli cgi]# cat echo_env.sh
echo_env(){
echo '$request_method:' $request_method
echo '</br>'
echo '$REQUEST_METHOD ' $REQUEST_METHOD
}
[root@moli cgi]#
[root@moli cgi]# cat index.cgi
#! /bin/bash
#
. ./cgi.sh
cgi
. ./echo_env.sh
echo_env
[root@moli nginx]# curl "http://www.moli.press:8888/cgi/" -X POST -d 'user_id=123&msg="OK!"'
post 方法
user_id=123&msg="OK!"
$request_method:
</br>
$REQUEST_METHOD POST
</br>$REQUEST_URI /cgi/
</br>$REQUEST_URI /cgi/
</br>$SCRIPT_NAME /cgi/index.cgi
</br>$DOCUMENT_ROOT /data/nginx/test_cgi
</br>$CONTENT_TYPE application/x-www-form-urlencoded
</br>$CONTENT_LENGTH 21
</br>$REMOTE_ADDR 47.103.206.151
</br>$HTTP_USER_AGENT curl/7.61.1
[root@moli ~]# curl "http://www.moli.press:8888/cgi/" -H "Content-Type:application/json" -X POST -d '{"user_id":"123","msg":"OK!"}'
post 方法
{"user_id":"123","msg":"OK!"}
$request_method:
</br>
$REQUEST_METHOD POST
</br>$REQUEST_URI /cgi/
</br>$REQUEST_URI /cgi/
</br>$SCRIPT_NAME /cgi/index.cgi
</br>$DOCUMENT_ROOT /data/nginx/test_cgi
</br>$CONTENT_TYPE application/json
</br>$CONTENT_LENGTH 29
</br>$REMOTE_ADDR 47.103.206.151
</br>$HTTP_USER_AGENT curl/7.61.1
6、金数据填写表单创建shell用户
- 注册金数据账号
- 创建账号密码表单
- 设置数据推送:设置数据推送的url
- 需要安装json解析工具:yum -y install jq // 解析json格式的一个工具
- 需要外网环境
- jq -h // 查看帮助
# user.sh
#! /bin/bash
#
add_user(){
local USER=`echo "$@" | jq -r .entry.field_1`
local PASS=`echo "$@" | jq -r .entry.field_2`
if [ ! -z "$USER" ];then
useradd $[USER] && echo "${PASS-nginx}" | passwd --stdin ${USER}
fi
}
[root@moli cgi]# cat index.cgi
#! /bin/bash
#
. ./cgi.sh
cgi
. ./echo_env.sh
echo_env
add_user "$POST_DATA"