Nginx和Perl的结合

 

    NginxPerl的结合中,同样是通过Nginx的配置文件中设置FastCGI代理来完成,而运行在后端的perl-FastCGI有三种可能,一种是perl-FastCGI运行于Unix套接字方式,那么在这种方式中,前端的Nginx和后端的perl-FastCGI必须运行在同一台服务器上;第二种是perl-FastCGI运行于TCP套接字,因此,基于第二种就有分为两种,一种是运行于本机,及前后台同机,而另一种为前后台不同机,所以共有三种方式。

 

三种结合方式

 

例子1

 

    在下面的一个例子中,前后台分别运行在两台不同的机器上,例如:

 

    前台Nginx服务器:

IP地址为 192.168.10.95

然后映射为外网IP,对外域名www.xx.com

 

后台perl-FastCGI

IP地址为 192.168.10.91

                     IP地址为 192.168.10.92

                     IP地址为 192.168.10.93

                     与前台同机unix:/var/run/nginx/perl_cgi-dispatch.sock;

 

     即后台的perl-FastCGI分布在3台不同的机器上。

 

    Nginx的配置文件中添加以下内容:

 

 

upstream backend  {

  server 192.168.10.91:8999;

  server 192.168.10.92:8999;

  server 192.168.10.93:8999;

  server unix:/var/run/nginx/perl_cgi-dispatch.sock;

}

 

server {

  listen       80;

  server_name  localhost;

  root  /var/www/;

  index index.html;

 

  location ~  \.(pl|cgi)$ {

    gzip off;

    fastcgi_index            index.cgi;

    include                 fastcgi.conf;

    fastcgi_read_timeout      5m;

    fastcgi_pass             backend;

 

}

   

    这个配置是一个轮询的实现,可以添加ip_hash指令,实现“粘贴”访问。

 

 

例子2

 

    在这个例子中,我们将测试缓存,为了看出效果,因此我们使用了两台后台服务器,即两台perl-FastCGI。环境是这样的:

 

    前台Nginx服务器:

IP地址为 192.168.10.95

然后映射为外网IP,对外域名www.xx.com

 

后台perl-FastCGI

IP地址为 192.168.10.91

                     IP地址为 192.168.10.92

 

    测试页面:

 

IP地址为 192.168.10.91perl-FastCGI服务器的页面内容为:

 

[root@ fastcgi1 www]# pwd

/var/www

[root@ fastcgi1 www]# cat test.pl

#!/usr/bin/perl

 

print "Content-type: text/html\n\n";

print "This is 192.168.10.91.";

 

IP地址为 192.168.10.91perl-FastCGI服务器的页面内容为:

    

[root@ fastcgi2 www]# pwd

/var/www

[root@ fastcgi2 www]# cat test.pl

#!/usr/bin/perl

 

print "Content-type: text/html\n\n";

print "This is 192.168.10.92.";

 

 

Nginx的配置文件中添加以下内容:

 

http {

  fastcgi_cache_path   /var/tmp/perl-FastCGI  levels=1:2

                       keys_zone=CACHE:10m

                       inactive=5m;

 

  fastcgi_cache_key "$scheme$request_method$host$request_uri";

 

upstream backend  {

    server 192.168.10.91:8999;

    server 192.168.10.92:8999;

}

 

server {

    listen       80;

    server_name  localhost;

    root  /var/www/;

    index index.html;

 

    location ~  \.(pl|cgi)$ {

      gzip off;

      fastcgi_index            index.cgi;

      include                 fastcgi.conf;

      fastcgi_read_timeout      5m;

      fastcgi_pass             backend;

      fastcgi_cache          CACHE;

      fastcgi_cache_valid    200 302  1h;

      fastcgi_cache_valid    301      1d;

      fastcgi_cache_valid    any      1m;

      fastcgi_cache_min_uses  1;

      fastcgi_cache_use_stale error  timeout invalid_header http_500;

}

   

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27043155/viewspace-732400/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27043155/viewspace-732400/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值