Nginx cgi-bin配置使用

  1. 安装cgi库fcgiwrap

编译安装:

git clone git://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make
make install

centos可以使用yum安装:

yum install fcgiwrap

安装以后fcgiwrap默认已经启动,对应的套接字是 /var/run/fcgiwrap.socket 。
如果没有启动,使用 /etc/init.d/fcgiwrap 手动启动。

  1. 配置nginx
location /cgi-bin/ {
                # Disable gzip (it makes scripts feel slower since they have to complete
                # before getting gzipped)
                gzip off;
                # Set the root to /usr/lib (inside this location this means that we are
                # giving access to the files under /usr/lib/cgi-bin)
                root /var/www;
                # Fastcgi socket
                fastcgi_pass unix:/var/run/fcgiwrap.socket;
                # Fastcgi parameters, include the standard ones
                include /etc/nginx/fastcgi_params;
                # Adjust non standard parameters (SCRIPT_FILENAME)
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}    
  1. 重启nginx
service nginx restart
  1. 编写程序

在 /var/www/cgi-bin/ 目录中创建文件 index.sh ,即,nginx中配置的目录下创建文件。

写入以下内容

#!/bin/bash
echo "Content-Type:text/html"
echo "" 
echo "hello world!"

脚本前三行是必须的
第一行用于指定脚本执行使用的解释器
第二行和第三行是HTTP协议规范,在发送HTML正文之前要发送MIME头和空行
因为脚本是执行文件,所以要给执行权限

为脚本设置可执行权限

chmod +x /var/www/cgi-bin/hello.sh
  1. 访问

在浏览器中打开:http://localhost/cgi-bin/index.sh
会输出:

hello world
  1. 另外一个展示cpu信息的例子

cpuinfo.sh

#!/bin/sh
# -*- coding: utf-8 -*-
NAME="cpuinfo"
echo -e "X-Test-Author: Michael\r\n"
echo "<html><head>"
echo "<title>$NAME</title>"
echo '<meta name="description" content="'$NAME'">'
echo '<meta name="keywords" content="'$NAME'">'
echo '<meta http-equiv="Content-type" content="text/html; charset=UTF-8">'
echo '<meta name="ROBOTS" content="noindex">'
echo "</head><body><pre>"
echo -e "`date`\n"
echo -e "uname -a"
uname -a
echo -e "\ncpuinfo"
#cat /proc/cpuinfo
echo "</pre></body></html>"

https://blog.csdn.net/hust_cxl/article/details/80091977

https://my.oschina.net/Corz/blog/491269

发表新评论