在Fedora 18上为Nginx提供CGI脚本
本教程将介绍如何在Fedora 18上为nginx提供CGI脚本(Perl脚本)。虽然nginx本身不提供CGI,但有几种方法可以解决这个问题。 我将概述两个解决方案:第一个是向CGI脚本代理请求,Thttpd是一个具有CGI支持的小型Web服务器,而第二个解决方案则使用CGI包装器来提供CGI脚本。
我不会保证这将为您工作!
1初步说明
我正在使用www.example.com
网站,其中包含文档根/var/www/www.example.com/web/
; vhost配置位于/etc/nginx/conf.d/www.example.com.vhost中
。
2使用Thttpd
在本章中,我将介绍如何将nginx配置为向Thttpd提供CGI脚本(扩展名.cgi
或.pl
)的代理请求。 我将配置Thttpd在端口8000上运行。
首先我们安装Thttpd。 Fedora 18有一个Thttpd软件包,但是nginx ThttpdCGI页面说Thttpd应该被修补 - 因此我们下载Fedora 18的src.rpm
软件包,修补它并从中构建一个新的rpm
软件包。
我们需要安装构建新的rpm
包所需的工具:
yum groupinstall 'Development Tools'
安装yum-utils
(该软件包包含允许我们下载src.rpm
的yumdownloader
工具):
yum install yum-utils
接下来我们下载Fedora 18的Thttpd src.rpm
软件包:
cd /usr/src
yumdownloader --source thttpd
ls -l
[root@server1 src]# ls -l
total 164
drwxr-xr-x. 2 root root 4096 Feb 3 2012 debug
drwxr-xr-x. 3 root root 4096 Jun 4 18:21 kernels
-rw-r--r-- 1 root root 155690 Mar 28 03:21 thttpd-2.25b-28.fc18.src.rpm
[root@server1 src]#
rpm -ivh thttpd-2.25b-28.fc18.src.rpm
您可以忽略以下警告:
[root@server1 src]# rpm -ivh thttpd-2.25b-28.fc18.src.rpm
1:thttpd warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
warning: user mockbuild does not exist - using root
warning: group mockbuild does not exist - using root
########################################### [100%]
[root@server1 src]#
现在我们将补丁下载到/ root / rpmbuild / SOURCES /
目录,并相应地修改/root/rpmbuild/SPECS/thttpd.spec
文件:
cd /root/rpmbuild/SOURCES/
wget -O thttpd-2.25b-ipreal.patch http://www.danielclemente.com/amarok/ip_real.txt
cd /root/rpmbuild/SPECS/
vi thttpd.spec
添加行Patch3:thttpd-2.25b-ipreal.patch
和%patch3 -p1 -b .ipreal
:
[...] Patch0: thttpd-2.25b-CVE-2005-3124.patch Patch1: thttpd-2.25b-fixes.patch Patch2: thttpd-2.25b-getline.patch Patch3: thttpd-2.25b-ipreal.patch [...] %prep %setup -q %patch0 -p1 -b .CVE-2005-3124 %patch1 -p1 -b .fixes %patch2 -p1 -b .getline %patch3 -p1 -b .ipreal [...] |
现在我们构建Thttpd rpm
包,如下所示:
rpmbuild -ba thttpd.spec
我们的Thttpd rpm
包是在/ root / rpmbuild / RPMS / x86_64
( / root / rpmbuild / RPMS / i386)
中创建的,如果你在i386系统上),那么我们去那里:
cd /root/rpmbuild/RPMS/x86_64
ls -l
[root@server1 x86_64]# ls -l
total 224
-rw-r--r-- 1 root root 69881 Sep 3 23:17 thttpd-2.25b-28.fc18.x86_64.rpm
-rw-r--r-- 1 root root 151685 Sep 3 23:17 thttpd-debuginfo-2.25b-28.fc18.x86_64.rpm
[root@server1 x86_64]#
安装Thttpd软件包,如下所示:
rpm -ivh thttpd-2.25b-28.fc18.x86_64.rpm
然后,我们对原始的/etc/thttpd.conf
文件进行备份,然后创建一个新的文件,如下所示:
mv /etc/thttpd.conf /etc/thttpd.conf_orig
vi /etc/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=thttpd 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的系统启动链接...
systemctl enable thttpd.service
...并启动它:
systemctl start thttpd.service
接下来创建/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/conf.d/www.example.com.vhost
...并向服务器{}
容器
添加位置/ cgi-bin {}
部分:
server { [...] location /cgi-bin { include proxy.conf; proxy_pass http://127.0.0.1:8000; } [...] } |
重新加载nginx:
systemctl reload nginx.service
因为Thttpd的文档根目录是/ var / www
,所以/ cgi-bin
将转换为目录/ var / www / cgi-bin
(这对所有的vhosts都是这样,这意味着每个vhost必须将其CGI脚本放在/ var / www / cgi-bin
;这是共享宿主环境的一个缺点;解决方案是使用第3章所述的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
如果一切顺利,您应该得到以下输出: