茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

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

    hello world

    一個簡單的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ū)別僅在于多了一個循環(huán)來接受請求,cgi::fast對象和cgi接口是一樣的,而且該腳本也可以當做cgi腳本使用。

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

    perl使用cgi::fast包來提供fastcgi服務,該包提供兩種方式來啟動fastcgi進程,一個是直接使用該包提供的服務將當前進程變?yōu)閒astcgi進程,另外一個是使用第三方工具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

    調試支持

    在前面的命令行里使用了參數-n,讓spawn-fcgi不要fork出多個進程,并阻塞,允許用戶ctrl+c來關閉,產品服務器可以去掉這個參數來充分利用服務器的多核來提供更高的并發(fā)數。我之前寫了一個bash腳本,允許在文件改動的情況下重啟服務,方便調試perl程序,代碼如下:

    代碼如下:

    #!/bin/bash

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

    pid_file=service.pid

    main=main.pl

    #關閉之前啟動的進程

    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

    }

    #當前腳本退出的時候也關閉啟動了的fastcgi進程

    trap term;exit sigint sigterm

    while true

    do

    #首次啟動或者文件改動后都需要關閉之前的進程

    term

    #以no fork方式啟動腳本來調試,并將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ā)離不開路由實現,來對不同請求來做出特定的響應。

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

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

    因此一個簡單的路由系統(tǒng)實際上可以分解為一個二級的map,注冊路由實際上就是往這個map里放入規(guī)則對應的處理函數,而分派請求則是從這個map里根據規(guī)則獲取對應的處理函數,一個簡單的例子:

    代碼如下:

    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 $@;

    }

    使用這個路由系統(tǒng)的例子:

    代碼如下:

    sub index

    {

    my ($q) = @_;

    print $q->header('text/plain');

    print hello world!;

    }

    router::add_rule('get', '/', \&index);

    模板系統(tǒng)

    perl提供了大量的模板系統(tǒng)的實現,我個人最喜歡的是template toolkit,文檔也非常豐富,網站是 。

    將前面的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文件內容如下:

    代碼如下:

    <html>

    <head><title>demo</title></head>

    <body>

    hello ${world}

    </body>

    </html>

    完!

    更多信息請查看腳本欄目

    2026國考·省考課程試聽報名

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