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

2025-05-26 0 54

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路径下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18
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

?

1

2

3

4

5

6

7

8

9

10
#!/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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 在ubuntu下为nginx配置支持cgi脚本的方案 https://www.kuaiidc.com/53606.html

相关文章

发表评论
暂无评论