在nginx下支持cgi腳本于支持node類似的,只要在nginx直接做個轉發,轉發到對應的cgi套接字就好。
使用Fcgiwrap
Fcgiqwrap是另外一個CGI封裝庫,跟Simple CGI類似。
安裝fcgiwrap
apt-get install fcgiwrap
安裝以后fcgiwrap默認已經啟動,對應的套接字是 /var/run/fcgiwrap.socket 。如果沒有啟動,使用 /etc/init.d/fcgiwrap 手動啟動。
配置nginx的vhost文件
在要支持cgi腳本的路徑下,添加對應的server配置。比如所有的cgi都在cgi-bin路徑下:
server {[...] 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/www.example.com; # 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; }[...]}
重新加載nginx:
nginx -s reload
測試
在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/www.example.com/cgi-bin/hello_world.cgi
在瀏覽器打開對應腳本,即可看到已經配置成功! http://www.example.com/cgi-bin/hello_world.cgi
新聞熱點
疑難解答