当前位置: 移动技术网 > 网络运营>服务器>nginx > 在ubuntu下为nginx配置支持cgi脚本的方案

在ubuntu下为nginx配置支持cgi脚本的方案

2019年04月19日  | 移动技术网网络运营  | 我要评论

昌都招标,武昌宏基客运站,美图多多

在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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网