在ubuntu下为nginx配置支持cgi脚本的方案
在nginx下支持cgi脚本于支持node类似的,只要在nginx直接做个转发,转发到对应的cgi套接字就好。
使用Fcgiwrap
Fcgiqwrap是另外一个CGI封装库,跟SimpleCGI类似。
安装fcgiwrap
apt-getinstallfcgiwrap
安装以后fcgiwrap默认已经启动,对应的套接字是/var/run/fcgiwrap.socket。如果没有启动,使用/etc/init.d/fcgiwrap手动启动。
配置nginx的vhost文件
在要支持cgi脚本的路径下,添加对应的server配置。比如所有的cgi都在cgi-bin路径下:
server{ [...] location/cgi-bin/{ #Disablegzip(itmakesscriptsfeelslowersincetheyhavetocomplete #beforegettinggzipped) gzipoff; #Settherootto/usr/lib(insidethislocationthismeansthatweare #givingaccesstothefilesunder/usr/lib/cgi-bin) root/var/www/www.example.com; #Fastcgisocket fastcgi_passunix:/var/run/fcgiwrap.socket; #Fastcgiparameters,includethestandardones include/etc/nginx/fastcgi_params; #Adjustnonstandardparameters(SCRIPT_FILENAME) fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name; } [...] }
重新加载nginx:
nginx-sreload
测试
在cgi-bin下创建hello-world.cgi
#!/usr/bin/perl-w #Tellperltosendahtmlheader. #Soyourbrowsergetstheoutput #ratherthen<stdout>(commandline #ontheserver.) print"Content-type:text/html\n\n"; #printyourbasichtmltags. #andthecontentofthem. print"<html><head><title>HelloWorld!!</title></head>\n"; print"<body><h1>Helloworld</h1></body></html>\n";
设置执行权限
chmod755/var/www/www.example.com/cgi-bin/hello_world.cgi
在浏览器打开对应脚本,即可看到已经配置成功!http://www.example.com/cgi-bin/hello_world.cgi