在Nginx和Perl的结合中,同样是通过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.91的perl-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.91的perl-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/