午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    perl使用nginx fastcgi環(huán)境做web開發(fā)實(shí)例
    來源:易賢網(wǎng) 閱讀:924 次 日期:2016-06-14 10:43:50
    溫馨提示:易賢網(wǎng)小編為您整理了“perl使用nginx fastcgi環(huán)境做web開發(fā)實(shí)例”,方便廣大網(wǎng)友查閱!

    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)來接受請求,cgi::fast對象和cgi接口是一樣的,而且該腳本也可以當(dāng)做cgi腳本使用。

    搭建nginx + fastcgi 環(huán)境

    perl使用cgi::fast包來提供fastcgi服務(wù),該包提供兩種方式來啟動fastcgi進(jìn)程,一個(gè)是直接使用該包提供的服務(wù)將當(dāng)前進(jìn)程變?yōu)閒astcgi進(jìn)程,另外一個(gè)是使用第三方工具spawn-fcgi來啟動。

    nginx配置方式例子:

    代碼如下:

    location / {

    fastcgi_pass 127.0.0.1:8184;

    fastcgi_param script_filename /scripts$fastcgi_script_name;

    include fastcgi_params;

    }

    配置好nginx后,使用spawn-fcgi來啟動前面的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腳本,允許在文件改動的情況下重啟服務(wù),方便調(diào)試perl程序,代碼如下:

    代碼如下:

    #!/bin/bash

    #pid文件和需要啟動的腳本

    pid_file=service.pid

    main=main.pl

    #關(guān)閉之前啟動的進(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)閉啟動了的fastcgi進(jìn)程

    trap term;exit sigint sigterm

    while true

    do

    #首次啟動或者文件改動后都需要關(guān)閉之前的進(jìn)程

    term

    #以no fork方式啟動腳本來調(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下測試通過

    路由系統(tǒng)

    做web開發(fā)離不開路由實(shí)現(xiàn),來對不同請求來做出特定的響應(yīng)。

    路由請求依賴http method和uri兩部分,因此主要就是需要這兩者來做分派。

    在cgi中可以通過環(huán)境變量request_method和request_uri來獲取請求方法和uri。

    因此一個(gè)簡單的路由系統(tǒng)實(shí)際上可以分解為一個(gè)二級的map,注冊路由實(shí)際上就是往這個(gè)map里放入規(guī)則對應(yīng)的處理函數(shù),而分派請求則是從這個(gè)map里根據(jù)規(guī)則獲取對應(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>

    完!

    更多信息請查看腳本欄目
    易賢網(wǎng)手機(jī)網(wǎng)站地址:perl使用nginx fastcgi環(huán)境做web開發(fā)實(shí)例
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國考·省考課程試聽報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)