perl使用nginx fastcgi環(huán)境做web開發(fā)實(shí)例

字號(hào):


    hello world
    一個(gè)簡單的hello world例子:
    代碼如下:
    #!/usr/bin/env perl
    use strict;
    use warnings;
    use cgi::fast;
    while(my $q = new cgi::fast)
    {
    print $q->header(text/plain);
    print hello world;
    }
    和cgi的區(qū)別僅在于多了一個(gè)循環(huán)來接受請(qǐng)求,cgi::fast對(duì)象和cgi接口是一樣的,而且該腳本也可以當(dāng)做cgi腳本使用。
    搭建nginx + fastcgi 環(huán)境
    perl使用cgi::fast包來提供fastcgi服務(wù),該包提供兩種方式來啟動(dòng)fastcgi進(jìn)程,一個(gè)是直接使用該包提供的服務(wù)將當(dāng)前進(jìn)程變?yōu)閒astcgi進(jìn)程,另外一個(gè)是使用第三方工具spawn-fcgi來啟動(dòng)。
    nginx配置方式例子:
    代碼如下:
    location / {
    fastcgi_pass 127.0.0.1:8184;
    fastcgi_param script_filename /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
    配置好nginx后,使用spawn-fcgi來啟動(dòng)前面的hello world:
    代碼如下:
    $ spawn-fcgi -n -a 127.0.0.1 -p 8184 -f ./main.pl
    調(diào)試支持
    在前面的命令行里使用了參數(shù)-n,讓spawn-fcgi不要fork出多個(gè)進(jìn)程,并阻塞,允許用戶ctrl+c來關(guān)閉,產(chǎn)品服務(wù)器可以去掉這個(gè)參數(shù)來充分利用服務(wù)器的多核來提供更高的并發(fā)數(shù)。我之前寫了一個(gè)bash腳本,允許在文件改動(dòng)的情況下重啟服務(wù),方便調(diào)試perl程序,代碼如下:
    代碼如下:
    #!/bin/bash
    #pid文件和需要啟動(dòng)的腳本
    pid_file=service.pid
    main=main.pl
    #關(guān)閉之前啟動(dòng)的進(jìn)程
    term() {
    test -e $pid_file || return
    pid=`cat $pid_file`
    kill -s -0 $pid || return
    echo terminating $main $pid
    rm -f $pid_file
    kill $pid
    wait $pid
    }
    #當(dāng)前腳本退出的時(shí)候也關(guān)閉啟動(dòng)了的fastcgi進(jìn)程
    trap term;exit sigint sigterm
    while true
    do
    #首次啟動(dòng)或者文件改動(dòng)后都需要關(guān)閉之前的進(jìn)程
    term
    #以no fork方式啟動(dòng)腳本來調(diào)試,并將pid寫入到文件
    spawn-fcgi -n -a 127.0.0.1 -p 8184 -f ./$main &
    pid=$!
    echo $pid > $pid_file
    echo my perl service started, pid = $pid
    #監(jiān)控文件變化
    files=`find . -name '*.pl' -o -name '*.pm' -o -name '*.html'`
    md5=`md5sum $files|md5sum`
    #wait for file change
    while [[ `md5sum $files|md5sum` = $md5 ]]
    do
    sleep 1
    done
    echo file changes detected, restarting service
    done
    該腳本已在mac osx和linux下測(cè)試通過
    路由系統(tǒng)
    做web開發(fā)離不開路由實(shí)現(xiàn),來對(duì)不同請(qǐng)求來做出特定的響應(yīng)。
    路由請(qǐng)求依賴http method和uri兩部分,因此主要就是需要這兩者來做分派。
    在cgi中可以通過環(huán)境變量request_method和request_uri來獲取請(qǐng)求方法和uri。
    因此一個(gè)簡單的路由系統(tǒng)實(shí)際上可以分解為一個(gè)二級(jí)的map,注冊(cè)路由實(shí)際上就是往這個(gè)map里放入規(guī)則對(duì)應(yīng)的處理函數(shù),而分派請(qǐng)求則是從這個(gè)map里根據(jù)規(guī)則獲取對(duì)應(yīng)的處理函數(shù),一個(gè)簡單的例子:
    代碼如下:
    my %routers = ();
    sub not_found
    {
    print status: 404\n;
    print content-type: text/html\n\n;
    print<<eof
    <html>
    <body>
    <h1>404 not found</h1>
    cannot find $env{request_path}.
    </body>
    </html>
    eof
    }
    sub add_rule
    {
    my ($method, $path, $callback) = @_;
    my $handlers = $routers{$method};
    $handlers = $routers{$method} = {} if not $handlers;
    $handlers->{$path} = $callback;
    }
    sub dispatch
    {
    my $q = shift;
    my $method = $env{request_method};
    my $uri = $env{request_uri};
    $uri =~ s/\?.*$//;
    my $handler = ($routers{$method} || {})->{$uri} || not_found;
    eval
    {
    &$handler($q);
    };
    print stderr failed to handle $method $uri: if $@;
    }
    使用這個(gè)路由系統(tǒng)的例子:
    代碼如下:
    sub index
    {
    my ($q) = @_;
    print $q->header('text/plain');
    print hello world!;
    }
    router::add_rule('get', '/', \&index);
    模板系統(tǒng)
    perl提供了大量的模板系統(tǒng)的實(shí)現(xiàn),我個(gè)人最喜歡的是template toolkit,文檔也非常豐富,網(wǎng)站是 。
    將前面的index修改為使用模板的例子:
    代碼如下:
    use template;
    my $tt = new template({include_path => 'templates', interpolate => 1});
    sub index
    {
    my ($q) = @_;
    my $output = '';
    print $q->header('text/html');
    $tt->process('index.html', {world => 'world'}, $output) || die $tt->error();
    print $output;
    }
    其中templates/index.html文件內(nèi)容如下:
    代碼如下:
    <html>
    <head><title>demo</title></head>
    <body>
    hello ${world}
    </body>
    </html>
    完!