Nginx 通过FCGI支持Perl 脚本

hosanna posted @ 2010年8月07日 17:23 in Web , 3144 阅读

折腾了几天,总算让Nginx也支持Perl程序了。因为之前已经配置好PHP的环境了(请看这里),所以这一次只是想在原来的的基础上增加Perl的支持而已,所以并不打算需要太复杂的设置。在网上找了很久,都没有找到比较好的教程,很多都需要自己写一个接口程序和启动脚本,并且很多都是不太符合Gentoo启动脚本的格式。最后没办法,只有自己慢慢的摸索了,参照PHP的相关设置,绕了不少的弯路,才总算成功了。趁自己还没有忘记之前,把过程记录下来吧。

首先,需要的是一个Perl的接口程序,找了好久我才找到了fcgiwrap这个程序,还好,Gentoo的Portage里也有,可以直接emerge它:

  1. $ eix fcgiwrap
  2. [*] www-misc/fcgiwrap
  3. Available versions: (~)1.0.2-r1 (**)9999
  4. Installed versions: 1.0.2-r1(2248472010年08月05日)
  5. Homepage: ht tp://nginx.localdomain.pl/wiki/FcgiWrap
  6. Description: Simple FastCGI wrapper for CGI scripts (CGI support for nginx)
  7. $ sudo emerge fcgiwrapp

创建一个spawn-fcgi启动脚本的链接

  1. $ sudo ln -s /etc/init.d/spawn-fcgi /etc/init.d/spawn-fcgi.pl

spawn-fcgi的配置文件

  1. $ cat /etc/conf.d/spawn-fcgi.pl
  2. # Copyright 1999-2009 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. # $Header: /var/cvsroot/gentoo-x86/www-servers/spawn-fcgi/files/spawn-fcgi.confd,v 1.6 2009/09/28 08:38:02 bangert Exp $
  5.  
  6. # DO NOT MODIFY THIS FILE DIRECTLY! CREATE A COPY AND MODIFY THAT INSTEAD!
  7.  
  8. # The FCGI process can be made available through a filesystem socket or
  9. # through a inet socket. One and only one of the two types must be choosen.
  10. # Default is the inet socket.
  11.  
  12. # The filename specified by
  13. # FCGI_SOCKET will be suffixed with a number for each child process, for
  14. # example, fcgi.socket-1.
  15. # Leave empty to use an IP socket (default). See below. Enabling this,
  16. # disables the IP socket.
  17. #
  18. FCGI_SOCKET=/var/run/fcgiwrap.sock
  19.  
  20. # When using FCGI_PORT, connections will only be accepted from the following
  21. # address. The default is 127.0.0.1. Use 0.0.0.0 to bind to all addresses.
  22. #
  23. #FCGI_ADDRESS=127.0.0.1
  24.  
  25. # The port specified by FCGI_PORT is the port used
  26. # by the first child process. If this is set to 1234 then subsequent child
  27. # processes will use 1235, 1236, etc.
  28. #
  29. FCGI_PORT=
  30.  
  31. # The path to your FastCGI application. These sometimes carry the .fcgi
  32. # extension but not always. For PHP, you should usually point this to
  33. # /usr/bin/php-cgi.
  34. #
  35. FCGI_PROGRAM=/usr/sbin/fcgiwrap
  36.  
  37. # The number of child processes to spawn. The default is 1.
  38. #
  39. #FCGI_CHILDREN=1
  40.  
  41. # If you want to run your application inside a chroot then specify the
  42. # directory here. Leave this blank otherwise.
  43. #
  44. #FCGI_CHROOT=
  45.  
  46. # If you want to run your application from a specific directiory specify
  47. # it here. Leave this blank otherwise.
  48. #
  49. #FCGI_CHDIR=
  50.  
  51. # The user and group to run your application as. If you do not specify these,
  52. # the application will be run as root:root.
  53. #
  54. FCGI_USER=nginx
  55. FCGI_GROUP=nginx
  56.  
  57. # Additional options you might want to pass to spawn-fcgi
  58. #
  59. FCGI_EXTRA_OPTIONS="-M 0700"
  60.  
  61. # If your application requires additional environment variables, you may
  62. # specify them here. See PHP example below.
  63. #
  64. ALLOWED_ENV="PATH"

Nginx配置文件

  1. $ cat /etc/nginx/vhosts/example.com.conf
  2. server {
  3. listen 127.0.0.1;
  4. server_name *.example.com example.com;
  5. access_log /var/log/nginx/localhost.access_log main;
  6. error_log /var/log/nginx/localhost.error_log info;
  7. root /var/www/example.com;
  8.  
  9. location ~ .*.php$ {
  10. include /etc/nginx/fastcgi.conf;
  11. fastcgi_pass 127.0.0.1:1234;
  12. fastcgi_param SCRIPT_FILENAME /var/www/example.com/$fastcgi_script_name;
  13. fastcgi_index index.php;
  14. }
  15.  
  16. location ~ .*.pl$ {
  17. include /etc/nginx/fastcgi_params;
  18. fastcgi_pass unix:/var/run/fcgiwrap.sock-1;
  19. fastcgi_param SCRIPT_FILENAME /var/www/example.com/$fastcgi_script_name;
  20. fastcgi_index index.pl;
  21. }

这里要注意的是,网上很多教程和fcgiwrap的手册(man page)都是把fastcgi_pass设置成 fastcgi_pass unix:/var/run/fcgiwrap.sock,但是这样是不行的,查看Nginx的Log会发现有提示如下的错误:
connect() to unix:/var/run/fcgiwrap.sock failed (2: No such file or directory) while connecting to upstream, client: 127.0.0.1, server: *.example.com, request: "GET /test.pl HTTP/1.1", upstream: "fastcgi://unix:/var/run/fcgiwrap.sock:", host: "example.com"

原因嘛,/etc/conf.d/spawn-fcgi.pl 这个配置文件里关于FCGI_SOCKET的注释有写到,FCGI_SOCKET会为每个子进程添加一个数字后缀,所以需要把这个后缀也要添加上去,例如:fastcgi_pass unix:/var/run/fcgiwrap.sock-1; 否则,就会出现文件找不到的错误,我在这里花了不少的时间才明白这个事情。

最后,启动相关服务:

  1. $ sudo /etc/init.d/spawn-fcgi.pl start
  2. $ sudo /etc/init.d/nginx start

如果还是出现502错语并且Nginx Log 里有这样的出错提示:upstream closed prematurely FastCGI stdout while reading response header from upstream, client: 127.0.0.1, server: *.example.com, request: "GET /test.pl HTTP/1.1", upstream: "fastcgi://unix:/var/run/fcgiwrap.sock-1:", host: "example.com",那就要检查一下你的Perl文件是否有读取跟执行的权限了(755),在这里又花了我很长的时间才明白Perl脚本是还需要执行的权限的,这一点跟PHP不同。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter