服务CGI脚本与Nginx在Debian Squeeze / Ubuntu 11.04

在Debian Squeeze / Ubuntu 11.04上为Nginx提供CGI脚本

本教程将介绍如何在Debian Squeeze / Ubuntu 11.04上为nginx提供CGI脚本(Perl脚本)。 虽然nginx本身不服务CGI,但有几种方法可以解决这个问题。 我将概述三个解决方案:第一个是将CGI脚本的代理请求发送给具有CGI支持的小型Web服务器Thttpd,而第二和第三个解决方案非常相似 - 都使用CGI包装器来提供CGI脚本。

我不会保证这将为您工作!

1初步说明

我正在使用网站www.example.com ,其中包含文件根/var/www/www.example.com/web/和Nginx vhost配置文件/etc/nginx/sites-enabled/www.example.com。虚拟主机

Ubuntu用户注意事项:

因为我们必须使用root权限运行本教程的所有步骤,所以我们可以使用字符串sudo在本教程中添加所有命令,也可以通过键入来成为root

sudo su

2使用Thttpd

在本章中,我将介绍如何将nginx配置为向Thttpd提供CGI脚本(扩展名.cgi.pl )的代理请求。 我将配置Thttpd在端口8000上运行。

首先我们安装Thttpd:

apt-get install thttpd

nginx ThttpdCGI页面说Thttpd应该打补丁 - 幸运的是,这个补丁已经包含在Debian和Ubuntu的thttpd包中,所以我们不需要这样做。

接下来我们打开/ etc / default / thttpd ...

vi /etc/default/thttpd

...并将ENABLED设置为yes

[...]
ENABLED=yes

然后,我们对原始的/ etc / thttpd / thttpd.conf文件进行备份,然后创建一个新的文件,如下所示:

mv /etc/thttpd/thttpd.conf /etc/thttpd/thttpd.conf_orig
vi /etc/thttpd/thttpd.conf
# BEWARE : No empty lines are allowed!
# This section overrides defaults
# This section _documents_ defaults in effect
# port=80
# nosymlink         # default = !chroot
# novhost
# nocgipat
# nothrottles
# host=0.0.0.0
# charset=iso-8859-1
host=127.0.0.1
port=8000
user=www-data
logfile=/var/log/thttpd.log
pidfile=/var/run/thttpd.pid
dir=/var/www
cgipat=**.cgi|**.pl

这将使Thttpd在127.0.0.1的 8000端口上监听 ; 其文档根目录是/ var / www

开始Thttpd:

/etc/init.d/thttpd start

接下来创建/etc/nginx/proxy.conf

vi /etc/nginx/proxy.conf
proxy_redirect          off;
proxy_set_header        Host            $host;
proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;

现在打开您的vhost配置文件...

vi /etc/nginx/sites-enabled/www.example.com.vhost

...并向服务器{} 容器添加位置/ cgi-bin {}部分:

server {
[...]
   location /cgi-bin {
      include proxy.conf;
      proxy_pass http://127.0.0.1:8000;
   }
[...]
}

重新加载nginx:

/etc/init.d/nginx reload

因为Thttpd的文档根目录是/ var / www ,所以/ cgi-bin将转换为目录/ var / www / cgi-bin (这对所有的vhosts都是这样,这意味着每个vhost必须将其CGI脚本放在/ var / www / cgi-bin ;这是共享宿主环境的一个缺点;解决方案是使用第3章和第4章所述的CGI包装器,而不是Thttpd)。

创建目录...

mkdir /var/www/cgi-bin

...然后将CGI脚本放在其中并使其可执行。 为了测试目的,我将创建一个小的Hello World Perl脚本(而不是hello_world.cgi,你也可以使用扩展名.pl - > hello_world.pl ):

vi /var/www/cgi-bin/hello_world.cgi
#!/usr/bin/perl -w

     # Tell perl to send a html header.
     # So your browser gets the output
     # rather then <stdout>(command line
     # on the server.)
print "Content-type: text/html\n\n";

     # print your basic html tags.
     # and the content of them.
print "<html><head><title>Hello World!! </title></head>\n";
print "<body><h1>Hello world</h1></body></html>\n";
chmod 755 /var/www/cgi-bin/hello_world.cgi

打开浏览器并测试脚本:

http://www.example.com/cgi-bin/hello_world.cgi

如果一切顺利,您应该得到以下输出:

赞(52) 打赏
未经允许不得转载:优客志 » 系统运维
分享到:

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏