Nginx invoke CGI Script

Source: Internet
Author: User

A small example

Python uses Cgihttpserver to invoke the shell as a CGI script

Mkdir-p/data/cgi-bin

Vim hello.sh

#!/bin/bashecho "content-type:text/html" echo "" echo "Hello world!"


Execute python-m cgihttpserver

Then enter http://<ip>:8000/cgi-bin/hello.sh in the browser


To invoke our CGI script

Description


1, the first three lines of the script is required, the line is used to specify the script to perform the interpreter used, the second and third lines are the HTTP protocol specification, before sending the HTML body to send a MIME header and a blank line


2. CGI scripts are executed using nobody users, so make sure that nobody can read and execute hello.sh


Enable CGI script under Nginx (Py,shell)


Installing fcgiwrap,spawn-fcgi,fcgi

Yum-y Install autoconf automake libtool fcgi fcgi-devel spawn-fcgi

1. Installing Fcgiwrap

wget Https://github.com/gnosek/fcgiwrap/archive/master.zip-O Fcgiwrap.zipunzip FCGIWRAP.ZIPCD Fcgiwrap-masterautoreconf-i #这里报错的话安装automake包./configure make do Install


2. Configure spawn-fcgi

Edit spawn-fcgi configuration file

vi/etc/sysconfig/spawn-fcgi Fcgi_socket=/var/run/fcgiwrap.socket Fcgi_program=/usr/local/sbin/fcgiwrap FCGI_USER= Nginx Fcgi_group=nginx fcgi_extra_options= "-M 0700" options= "-u $FCGI _user-g $FCGI _group-s $FCGI _socket-s $FCGI _extra_ Options-f 1-p/var/run/spawn-fcgi.pid--$FCGI _program "


Add boot up:

Chkconfig spawn-fcgi on

To start the spawn-fcgi service:

Service spawn-fcgi Start


Modified CGI application (this is a tool that my colleague wrote with Py to modify the system time)

Tar zxvf cgi-bin.tar.gz-c/data

Modify the Herf address inside the index.py


Set Nginx


CGI application in the/data/cgi-bin directory


server {                 root /data;                  location ~* . (py|sh) $ {                     gzip off;                     fastcgi_pass unix:/ var/run/fcgiwrap.socket;                     include fastcgi_params;                     fastcgi_param script_ filename  $document _root$fastcgi_script_name;                  }                 location ~* . (JPG|GIF|PNG|JS|CSS) $ {                 if  (-f  $request _filename)  {                         expires  max;                                 break;                                 }                            }         } 

Re-start Nginx

Service Nginx Reload


Access http://10.10.30.177/cgi-bin/index.py to invoke our CGI script

http://10.10.30.177/cgi-bin/hello.sh here corresponds to the above CGI example



This article from "Do not abandon!" Do not give up "blog, be sure to keep this source http://thedream.blog.51cto.com/6427769/1718527

Nginx invoke CGI Script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.