Sun Java System Web Server 7.0 Update 3 管理員指南

附錄 B FastCGI 外掛程式

簡介

FastCGI 為現有 CGI (共用閘道介面) 的增強版本,CGI 是外部應用程式與 Web 伺服器之間的介面標準。與 CGI 一樣,FastCGI 應用程式以獨立的隔離程序執行。使用 FastCGI 的一些優勢如下:

FastCGI 外掛程式可讓 Web 伺服器以可延伸的方式,安全地與常用協力廠商動態內容產生技術 (例如 Perl 和 Python) 搭配作業。

如需有關 FastCGI 的更多資訊,請參閱 http://www.fastcgi.com/devkit/doc/fcgi-spec.html 上的規格。

外掛程式函數 (SAF)

FastCGI 外掛程式提供了下列伺服器應用程式函數 (SAF):

以下小節說明 FastCGI SAF 的各種參數和「error-reason」字串:

auth-fastcgi

auth-fastcgi 是一個 PatchCheck 函數。此函數用於將請求轉寄至「Authorizer」FastCGI 應用程式。如果授權成功,將傳送回覆碼 200。否則,會將來自「Authorizer」FastCGI 應用程式的回應傳送回使用者代理程式。

您可以在 http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S6 找到有關 FastCGI 角色的更多資訊。

auth-fastcgi SAF 可接受的參數位於: FastCGI SAF 參數

以下 obj.conf 程式碼範例說明 auth-fastcgi 的用法:

PathCheck fn="auth-fastcgi" app-path="/usr/bin/perl" app-args="/fastcgi/apps/auth/SimpleAuth.pl" bind-path="localhost:3432"

responder-fastcgi

responder-fastcgi 是一個 Service 函數。此函數用於將請求轉寄至做為「Responder」的 FastCGI 應用程式。來自 Responder 應用程式的回應將傳送回使用者代理程式。可以在 http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S6 找到有關 FastCGI 角色的更多資訊。

responder-fastcgi SAF 可接受的參數清單位於: FastCGI SAF 參數

以下 obj.conf 程式碼範例說明 responder-fastcgi 的用法:

Service fn="responder-fastcgi" app-path="/fastcgi-enabled-php-installation/bin/php" bind-path="localhost:3433" app-env="PHP_FCGI_CHILDREN=8" app-env="PHP_FCGI_MAX_REQUEST=500"

filter-fastcgi

filter-fastcgi 是一個 Service 函數。此函數用於將請求轉寄至「Filter」類型的 FastCGI 應用程式。「Filter」應用程式可接收與 HTTP 請求關聯的資訊,還可以接收儲存在伺服器上的檔案中資料。然後,「Filter」應用程式產生「已篩選」版本的資料流做為回應。此回應將被傳送回使用者代理程式。可以在 http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S6 找到有關 FastCGI 角色的更多資訊。

filter-fastcgi SAF 可接受的參數清單位於: FastCGI SAF 參數

以下 obj.conf 程式碼範例說明 filter-fastcgi 的用法:

Service fn="filter-fastcgi" app-path="/fastcgi/apps/filter/SimpleFilter" bind-path="localhost:3434" app-env="LD_LIBRARY_PATH=/fastcgi/fcgi-2.4/libfcgi/.libs" min-procs=2

error-fastcgi

error-fastcgi 是 Error 函數。error-fastcgi SAF 可處理 FastCGI 外掛程式特有的錯誤。但是,此函數並不處理 HTTP 錯誤。可將 FastCGI 外掛程式配置為發生錯誤時顯示特定頁面,或將請求重新導向至特定 URL。

error-fastcgi SAF 可接受的參數清單位於: FastCGI SAF 參數

以下 obj.conf 程式碼片段示範 error-fastcgi 的用法:

Error fn="error-fastcgi" error-reason="Invalid Parameters" error-url="http://www.foo.com/errorPage.html"

請參閱FastCGI SAF 參數,以取得有關 error-fastcgi 參數的資訊。

FastCGI SAF 參數

除非另有明確說明,否則 FastCGI 外掛程式 SAF「auth-fastcgi」、「responder-fastcgi」和「filter-fastcgi」均可接受下列參數:

請注意,chrootusergroupnice chdirrlimit_asrlimit_corerlimit_nofile 等參數僅適用於 UNIX 平台。這些參數在 Windows 平台上會被忽略。

error-fastcgi 伺服器應用程式功能 (SAF) 接受下列參數:

error-fastcgi SAF 錯誤原因字串

本節提供所有有效「error-reason」字串的清單及這些字串的說明:

配置 Web 伺服器的 FastCGI 外掛程式

FastCGI 外掛程式隨附於 Web Server 7.0。您可以透過下列一種方式在 Web 伺服器上配置 FastCGI 外掛程式:

手動配置 Web 伺服器的 FastCGI 外掛程式

外掛程式安裝於下列位置:

32 位元的 FastCGI 外掛程式二進位檔安裝在 <install_dir>/plugins/fastcgi 目錄下。

64 位元的 Solaris SPARC FastCGI 外掛程式二進位檔安裝在 <install_dir>/lib/plugins/fastcgi/64 目錄下。

將安裝下列 FastCGI 二進位檔:

libfastcgi.so (適用於 Solaris/Linux)

fastcgi.dll(適用於 Windows)

Fastcgistub.exe(適用於 Windows)

libfastcgi.sl(適用於 HP-UX)

Fastcgistub(可執行檔)

配置 FastCGI 外掛程式時,需使用位於 <instance-dir>/config 目錄下的 Web 伺服器配置檔案。若要配置 FastCGI 外掛程式,請執行下列步驟:

修改 magnus.conf

使用「load-modules」Init 函數載入 FastCGI 外掛程式共用程式庫。

Init fn=flex-init access="access" format.access="%Ses->client.ip%
- %Req->vars.auth-user% [%SYSDATE%] \"%Req->reqpb.clf-request%\"
%Req->srvhdrs.clf-status% %Req->srvhdrs.content-length%"

Init fn="load-modules" shlib="libJava EEplugin.so" shlib_flags="(global|now)"

Init fn="load-modules" shlib="libfastcgi.so" shlib_flags="(global|now)"

修改 MIME 類型 (選擇性)

編輯 mime.types 檔案以指定 MIME 對映。修改 MIME 類型對映是可選步驟。

例如,

#--Sun Microsystems Inc. MIME Information

# Do not delete the above line. It is used to identify the file type.

#

# Copyright 2006 Sun Microsystems, Inc. All rights reserved.

# Use is subject to license terms.

#


type=application/octet-stream exts=bin

type=application/astound exts=asd,asn

...

...

type=magnus-internal/fastcgi exts=php

...

...

修改 obj.conf

編輯 obj.conf 檔案,以使用之前各節所述的外掛程式 SAF,來配置 FastCGI 特有的請求。

修改後的 obj.conf 檔案範例如下所示:

#

# Copyright 2006 Sun Microsystems, Inc. All rights reserved.

# Use is subject to license terms.

#


# You can edit this file, but comments and formatting changes

# might be lost when you use the administration GUI or CLI.



<object name = "default">

		AuthTrans fn="match-browser" browser="*MSIE*" 
						ssl-unclean-shutdown="true"
		NameTrans fn="ntrans-Java EE" name="Java EE"
		NameTrans fn="pfx2dir" from="/mc-icons" 
						dir="/ws7/lib/icons" name="es-internal"
		NameTrans fn="assign-name" from="/fcgi/*" name="fcgi.config"

</object>

<Object name="fcgi.config">

		AuthTrans fn="auth-fastcgi" app-path="/fastcgi/apps/c/simpleAuth" 
				bind-path="localhost:2111"
		Service fn="responder-fastcgi" 
						app-path="/fastcgi_enabled_php_installation_dir/bin/php" 
				app-env="name1=abc"

</object>
...

請注意,針對不同的 URL 樣式定義不同的物件或將 SAF 對映至不同的 MIME 類型,便可以不同的方式呼叫 FastCGI SAF。

如需有關 obj.conf 配置與語法的更多資訊,請參閱 「Sun Java System Web Server 7.0 Update 3 Administrator’s Configuration File Reference」中的第 6 章「Syntax and Use of obj.conf」

配置多個 FastCGI 應用程式

您無法透過管理主控台或 CLI 配置多個 FastCGI 應用程式。為解決此問題,您可以修改 obj.conf 檔案,藉以配置多個應用程式。例如:

<If> $uri =~ '^/fcgi/(.*)'>

Service fn="responder-fastcgi" app-path="/export/home/bits/fastcgi/fcgi-2.4.0/examples/$1" app-env="LD_LIBRARY_LIBRARY_PATH=/export/home/bits/fastcgi/fcgi-2.4.0/libfcgi/.libs"</If>

此表示式會建立不需個別配置的 <app-path> 程序。


備註 –

您不可對多個應用程式配置相同的 bind-path。這會導致啟動作業因 bind-path 相同而失敗。


配置虛擬主機環境

虛擬主機環境的用途,在於防止因與數個虛擬伺服器共用 PHP 引擎,所產生的潛在安全性與效能問題。

使用 Web Server 7.0 環境變數,即可指定與連結至每部虛擬伺服器的個別引擎相同的 PHP 二進位檔。您應確定每部虛擬伺服器皆有自己的 php.ini 檔案。


Service fn=responder-fastcgi
        app-path="/path/to/php/php_fcgi"
        bind-path="$(lc($urlhost))"
        req-retry=5
        type="*magnus-internal/fastcgi*"
        app-env="PHPRC=/path/to/users/$(lc($urlhost))/config"
        app-env="PHP_FCGI_CHILDREN=5"
        app-env="PHP_FCGI_MAX_REQUEST=200"
        min-procs=1
        restart-interval=10
        bucket="php-bucket"
        rlimit_cpu=60

Web 伺服器 tmp 目錄此時會顯示 Unix 網域通訊端,這些通訊端的名稱由來是處理 PHP 請求的個別虛擬伺服器。對所有使用者使用單一 PHP FastCGI 二進位檔,即會出現此情形。因此,所有以此單一二進位檔編譯的必要外掛程式,都將由該二進位檔進行處理掌控。上述問題的解決方案就是確定每位使用者皆有符合其本身需求的 PHP 二進位檔副本。


Service fn=responder-fastcgi
        app-path="/path/to/users/$(lc($urlhost))/php_fcgi"
        bind-path="$(lc($urlhost))"
        req-retry=5
        type="*magnus-internal/fastcgi*"
        app-env="PHPRC=/path/to/users/$(lc($urlhost))/config"
        app-env="PHP_FCGI_CHILDREN=5"
        app-env="PHP_FCGI_MAX_REQUEST=200"
        min-procs=1
        restart-interval=10
        bucket="php-bucket"
        rlimit_cpu=60

您也可以控制 URI 空間的結構,而讓每個應用程式使用不同的 PHP 二進位檔。

例如:

若 URI 空間的結構如下:

/app/foo.php

其中,/app 是整體應用程式的名稱,且在以 PHP 檔案結尾的 URI 結構中一律為第一個目錄。


<If uri~=^/(\w+)/\w+\.php$>
						Service fn=responder-fastcgi
						app-path="/path/to/users/$(lc($urlhost))/$1/php_fcgi"
						bind-path="$(lc($urlhost))_$1"
						req-retry=5
						type=+magnus-internal/fastcgi*"
						app-env="PHPRC=/path/to/users/$(lc($urlhost))/config"
						app-env="PHP_FCGI_CHILDREN=5"
						app-env="PHP_FCGI_MAX_REQUEST=200"
						min-procs=1
						restart-interval=10
						bucket="php-bucket"
						rlimit_cpu=60
</If>

這會呼叫一個特別建置的 PHP FastCGI 二進位檔,而該檔案會連結至具有唯一名稱的 Unix 網域通訊端。因此,這不會影響到其他 PHP 應用程式或虛擬伺服器。但是,由於此程序伴隨著許多 PHP 程序,因此會耗用大量的記憶體。

配置檔案範例

以下是使用 FastCGI 配置 PHP 的配置檔案範例。

<If -f $path>
Service type="magnus-internal/php"
    fn="responder-fastcgi"
    app-path="/opt/coolstack/php5/bin/php-cgi"
    bind-path="localhost:3101"
    app-env="PHPRC=/opt/coolstack/php5"
    app-env="PHP_FCGI_CHILDREN=5"
    app-env="PHP_FCGI_MAX_REQUEST=200"
    app-env="FCGI_WEB_SERVER_ADDRS=127.0.0.1"
    req-retry=5
    restart-interval=10
    bucket="php-bucket"

</If>
<Else>
Service type="magnus-internal/php" fn="set-variable" error="404"
</Else>

FastCGI 外掛程式疑難排解

Fastcgistub 是一個程序管理程式,用於管理 FastCGI 應用程式程序的生命週期。Fastcgistub 將其訊息記錄至 Web 伺服器暫存目錄下的 Fastcgistub.log 檔案中。發生任何錯誤時,檢查此檔案有助於消除問題。

問題:FastCGI 請求未得到處理

可能的原因及解決方案如下:

  1. 請檢查是否載入 FastCGI 外掛程式。如果在啟動 Web 伺服器期間出現下列訊息,則表示已成功載入外掛程式。另外,請檢查 magnus.conf 中的外掛程式程式庫的路徑:FCGI1000: Sun Java System Web Server 7.0 Update 3 FastCGI NSAPI Plugin <建置資訊>

  2. 請檢查在 obj.conf 中是否正確指定請求對映。如需有關 obj.conf 檔案的更多資訊,請參閱「Sun Java System Web Server Administrator's Configuration Reference File」。

  3. 請檢查錯誤記錄中的任何可能錯誤訊息。

  4. 請檢查 stub 二進位檔及 FastCGI 應用程式的權限。如果未授予足夠的權限,則外掛程式將無法啟動 stub 或應用程式。有關

  5. 請檢查 Fastcgistub.log 檔案中有關 stub 端的任何可能錯誤。您可以在 <instances>/logs 中找到記錄的詳細資訊。

  6. 如果可能,請以獨立模式執行 FastCGI 應用程式,檢查該應用程式是否能夠正確無誤地執行。

如果丟出任何程式庫相依性錯誤,請在 obj.conf 中將 LD_LIBRARY_PATH 指定為具有 LD_LIBRARY_PATH=<相依性程式庫路徑> 值的 app-env 參數。

問題:FastCGI 應用程式未啟動。

可能的原因及解決方案如下:

請檢查 Fastcgistub.log 檔案中的下列記錄訊息:

..
<pid> process startup failure, trying to restart
...
Even after trying <n> time(s), <application path> process failed to start...no more retries

啟動失敗的原因之ㄧ可能是由於無法載入相依性程式庫。此問題可透過以下方式解決:將適當的程式庫路徑當成 app-env 參數值,指定給 obj.conf 檔案中配置的 FastCGI 應用程式。例如:


Service fn="responder_fastcgi" app-path="/fastcgi/c/tux-app" bind-path="localhost:2112" 
app-env="LD_LIBRARY_PATH=/tuxedo/lib"

開發 FastCGI 應用程式

您可以使用 Perl、PHP、C 和 Java 開發 FastCGI 應用程式。下列小節簡要說明使用一些常用程式設計語言開發應用程式的程序。

Procedure執行 FastCGI 應用程式

  1. 停止 Web 伺服器。

  2. 重新啟動 Web 伺服器。

  3. 存取以「fcgi」做為應用程式根的應用程式。

    例如: http://localhost/fcgi/ListDir.php

FastCGI 應用程式結構

一般 FastCGI 應用程式具有以下程式碼結構:

Initialization code

Start of response loop
		body of response loop
End of response loop

初始化程式碼僅在應用程式初始化時執行一次。初始化程式碼通常執行一些耗時作業,例如開啟資料庫或者計算表格或點陣圖的值。將 CGI 程式轉換為 FastCGI 程式的主要作業,是區分初始化程式碼與針對每個請求都需要執行的程式碼。

回應迴圈會持續執行,等待用戶端請求到達。迴圈以呼叫 FCGI_Accept (FastCGI 程式庫中的常式) 開頭。FCGI_Accept 常式將封鎖程式執行,直到用戶端請求 FastCGI 應用程式為止。收到用戶端請求時,FCGI_Accept 將解除封鎖、執行回應迴圈主體的一次重複運算,之後再次封鎖,等待另一個用戶端請求。僅當系統管理員或 Web 伺服器強制結束 FastCGI 應用程式時,此迴圈才會終止。

使用 Perl

從 CPAN 下載並安裝最新的 FCGI 模組。就 ActivePerl 而言,您可以從 http://aspn.activestate.com/ASPN/Downloads/ActivePerl/PPM/Zips 下載模組。

如需有關使用 Perl 撰寫 FastCGI 應用程式的更多資訊,請參閱 http://www.fastcgi.com/#TheDevKit

使用 PHP

從 PHP 4.3.0 開始,FastCGI 成為 PHP 引擎支援的配置。若要編譯支援 FastCGI 之 PHP 4.3.x 或更高版本的引擎,請將配置切換 --enable-fastcgi 納入建立程序,例如


./configure <other-options> --enable-fastcgi
gmake

編譯完成後,php 二進位檔將啟用 FastCGI。

當使用 PHP 版本 5.1.2 或舊版 (包括 PHP 4.x) 時,應使用 host: port 格式配置 FastCGI 外掛程式。例如,bind-path = “localhost: 3333”。

對於 PHP 版本 5.1.3 及更高版本,bind-path 是可選擇的。如果已指定,則不應使用「host: port」格式。它可以是一個字串。例如,bind-path = “myphpbindpath”。

使用 C/Java

FastCGI 開發工具組提供撰寫 FastCGI C/Java 應用程式的 API。您可以從 http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm 下載工具組。

若要建立下載的 FastCGI 開發套件,請執行下列步驟:

  1. 解壓縮 tar 檔案。此動作將建立名為 fcgi-devel-kit 的新目錄

  2. fcgi-devel-kit 目錄中執行此指令序列:

    1. ./configure

    2. make

如需有關使用 C 撰寫 FastCGI 應用程式的更多資訊,請參閱 http://www.fastcgi.com/devkit/doc/fcgi-devel-kit.htm#S3

如需有關使用 Java 撰寫 FastCGI 應用程式的更多資訊,請參閱 http://www.fastcgi.com/devkit/doc/fcgi-java.htm

透過管理主控台配置 Web 伺服器的 FastCGI 外掛程式

Procedure透過管理主控台配置 FastCGI 外掛程式

  1. http://www.sun.com/download/index.jsp 下載啟用 FastCGI 的 Sun Java System Web Server 7.0 PHP Add-On 1.0

  2. 將 PHP 配置為 Web 伺服器上的 FastCGI 伺服器。

    1. phppack-5_2_0*.zip 解壓縮至 /export/home


      $ cd /export/home; unzip phppack-5_2_0*.zip
    2. 啟動管理伺服器。


      $ <webserver-install-root>/admin-server/bin/startserv
    3. 使用管理主控台配置 FastCGI 處理程式。

      1. 登入管理主控台。

      2. 按一下 [虛擬伺服器作業] 中的 [編輯虛擬伺服器]。

      3. 按一下 [虛擬伺服器一般特性] 中的 [內容處理] 標籤。

      4. 按一下 [內容處理 — 一般特性] 中的 [FastCGI] 標籤。

      5. 按一下 [新增] 按鈕,使用 FastCGI 處理程式對映增加新的 URI。

        輸入下列值:

        • 套用至:選取 [新增 URI] 並輸入 /fastcgi/*

        • 角色:從下拉式清單中選取 [回應程式]

        • 應用程式路徑:輸入 /export/home/php/bin/php 做為路徑。

        • 環境變數:輸入變數:


          "PHPRC=/export/home/php","LD_LIBRARY_PATH=/export/home/php",
          "LD_LIBRARY_PATH_64=/export/home/php/64"
      6. 按一下 [確定] 按鈕。如果需要,您可以按一下配置的 [部署] 按鈕。

  3. 建立符號連結。


    $ ln -s <webserver-install-root>/samples/fastcgi <webserver-instance-docroot>
  4. 執行範例。

    • Hello World URL 範例

      http://<host-name>:<webserver-instance-port>/fastcgi/HelloWorld.php

    • 目錄清單 URL 範例

      http://<host-name>:<webserver-instance-port>/fastcgi/directory.php

    • 頁面計數器 URL 範例

      http://<host-name>:<webserver-instance-port>/fastcgi/pageCounter.php

    • 伺服器資訊 URL 範例

      http://<host-name>:<webserver-instance-port>/fastcgi/serverinfo.php

透過 CLI 配置 Web 伺服器的 FastCGI 外掛程式

FastCGI 處理程式共有五個相關的 CLI 指令,如下所示:

Procedure透過 CLI 配置 FastCGI 外掛程式

  1. 呼叫下列指令以建立 FastCGI 處理程式。


    wadm> create-fastcgi-handler --config=test --vs=test --uri-pattern=/php/* --role=filter
     --app-path=C:\\php\\phppack-5_2_0-windows-i586\\php\\php-cgi.exe

    此時會建立具有 [篩選器] 角色的 FastCGI 處理程式。

    如需更多資訊,請參閱 CLI 參考資料 create-fastcgi-handler(1)

  2. 呼叫指令以部署配置。


    wadm> deploy-config test

    備註 –

    如果您是第一次建立 FastCGI 處理程式,則應在部署配置後重新啟動實例。


    wadm> restart-instance --config=test localhost

以遠端模式執行啟用 FastCGI 的 PHP 應用程式

您可以用遠端模式執行啟用 FastCGI 的 PHP,並配置 Sun Java System Web Server。這可讓 Web 伺服器將請求傳送至遠端 PHP 引擎。

Procedure執行啟用 FastCGI 的 PHP 應用程式

  1. 執行啟用 FastCGI 的 PHP。


    $ php -b <hostname>:<port> &

    例如:


    $ php -b localhost:4321 &

    備註 –

    您可以執行下列指令,以檢查您所使用的 PHP 是否為啟用 FastCGI 的 PHP:


    $ php -v

    PHP 5.2.5 (cgi-fcgi) (built: May  8 2008 12:50:19)
    Copyright (c) 1997-2007 The PHP Group
    Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

    在輸出中找出 cgi-fcgi 以進行確認。


  2. 使用 CLI 配置 Sun Java System Web Server。

    例如,會建立名為 test 的 Web 伺服器實例。

  3. 使用 CLI 執行下列指令:


    wadm> create-fastcgi-handler --config=test --vs=test
     --uri-pattern=/php/* --role=responder --bind-path="localhost:4321"
    wadm> deploy-config test

    此時會建立具有 [回應程式] 角色的 FastCGI 處理程式。

  4. 重新啟動您的實例。


    wadm> restart-instance --config=test localhost

    完成配置後,您可以檢查系統是否已將來自 Web 伺服器的請求轉送至遠端 PHP 引擎。

    1. 將下列 PHP 程序檔範例置於實例文件根的 php 子目錄中,即 <instance-dir>/docs_directory


      info.php:
      <?php
      phpinfo();
      ?>
    2. 存取遠端 PHP 引擎 URL:http://localhost:<webserverport>/php/info.php,以驗證請求的狀態。

FastCGI 應用程式範例

本節包含使用 PHP、Perl 和 C 撰寫的 FastCGI 應用程式範例。

使用 PHP 撰寫的回應程式應用程式 (ListDir.php)

<?php
 		$dir = "/tmp/";

		// Open a known directory, and proceed to read its contents
		if (is_dir($dir)) {
			if ($dh = opendir($dir)) {
 				while (($file = readdir($dh)) !== false) {
 					echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
 				}
				closedir($dh);
			}

		}
?>

上述範例的 obj.conf 程式碼片段:

<Object name="default">
		NameTrans fn="assign-name" from="/fcgi/*" name="responder.fcgi"
</Object>
<Object name="responder.fcgi">
		Service fn="responder-fastcgi" app-path="/foo/fastcgi-enabled-php-installation/bin/php" 
			bind-path="localhost:3431"  min-procs=3
</Object>

使用 Perl 撰寫的 Authorizer 應用程式 (SimpleAuth.pl)

#!/usr/bin/perl

use FCGI;

while (FCGI::accept >= 0) {
		if( $ENV{'HTTP_AUTHORIZATION'} ) { 
        # This value can be further decoded to get the actual 
			# username and password and then
        # perform some kind of user validation. This program only 
        # checks for the presence of
        # of this environment param and is not really bothered about its value

        print( "Status: 200\r\n" );
        print( "\r\n" );

    } else {
       
        print( "Status: 401\r\n" );
        print( "WWW-Authenticate: basic realm=\"foo\"\r\n" );
        print( "\r\n" );  

   }

}

上述範例的 obj.conf 設定:

<Object name="responder.fcgi">
		AuthTrans fn="auth-fastcgi" app-path="/fastcgi/apps/auth/SimpleAuth.pl" 
		bind-path="localhost:3432"
		Service fn="responder-fastcgi" app-path="/foo/fastcgi-enabled-php-installation/bin/php" 
		bind-path="localhost:3433" app-env="PHP_FCGI_CHILDREN=8" min-procs=1
</Object>

第一次向 http://localhost/fcgi/php/ListDir.php 提出請求時,瀏覽器會顯示認證對話方塊。在使用者輸入使用者名稱和密碼後,即會列出「/tmp」目錄內容。

使用 C 撰寫的 Filter 應用程式 (SimpleFilter.c)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcgi_stdio.h>

		void main(void) {
			size_t PageSize = 1024 * 3;
    		char *page;
    		FCGX_Stream *in, *out, *err;
    		FCGX_ParamArray envp;

		 	int count=0;
    		page = (char *)malloc(PageSize);

		 	if (page == NULL) {

				printf("Content-type: text/x-server-parsed-html\r\n");
				printf("<title>malloc failure</title>");
				printf("<h1>Cannot allocate memory to run filter. exiting</h1>");
				printf("\r\n\r\n");
				exit(2);
			}

			while(FCGI_Accept() >= 0) {

         	char *tmp;
         	char *execcgi;      
         	char *dataLenStr = NULL;
         	int numchars = 0;
         	int stdinDataSize = 0;
         	int filterDataLen = 0;
         	int dataToBeRead = 0;
         	int x = 0;
         	int loopCount = 0;   
				

			  	count++;
         	dataLenStr = getenv("FCGI_DATA_LENGTH");
        
         	if(dataLenStr)
             	filterDataLen = atoi(dataLenStr);
   
				/* clear out stdin */
         	while (EOF != getc(stdin)) {
             	stdinDataSize++;
         	}

				dataToBeRead = filterDataLen;
        	FCGI_StartFilterData();
        	tmp = page; /** just in case fread or fwrite moves our pointer **/


				//start responding
				printf("Content-type: text/plain\r\n");
				printf("\r\n"); /** send a new line at the beginning **/
				printf("<title>SIMPLE FILTER</title>");
				printf(<h1>This page was Filtered by SimpleFilter FastCGI filter</h1>");
				printf("file size=%d<br>", filterDatalen);
				printf("stdin size=%d<br>, stdinDataSize);


				while(dataToBeRead > 0 ) {
           		x = 0;
           		page = tmp;
            
           		if(dataToBeRead > PageSize)
             		 x = PageSize;
           		else
              		 x = dataToBeRead;
					numchars = fread((void *)(page), 1, x, stdin);
				
					if( numchars == 0 )
						continue; 
					/** at this point your data is in page pointer, so do 
					whatever you want 
               with it before sending it back to the server.
					In this example, no data is manipulated. Only the count of number of 
               times the filter data is read and the total bytes read 
					at the end of every 
               loop is printed. **/	

					dataToBeRead -= numchars;
					loopCount++;
					printf("loop count = %d ... so far read %d bytes <br>", loopCount, 
					(filterDatalen - dataToBeRead));
				}
				printf("\r\n\r\n"); /** send a new line at the end of transfer **/

				fflush(stdout);

				page = tmp; /** restore page pointer **/
				memset(page,NULL,numchars);
		}

		free(page);
}

上述範例的 obj.conf 設定範例。

如果此 FastCGI 應用程式和 Web 伺服器在同一台機器上執行,則

<Object name=<"filter.fcgi">
						Service fn="filter-fastcgi" app-path="/fastcgi/apps/filter/SimpleFilter.exe" 
             bind-path="localhost:3434" app-env="LD_LIBRARY_PATH=/fastcgi/fcgi-2.4/libfcgi/.libs"
</Object>

如果該應用程式是在遠端機器上執行,則 obj.conf 檔案中必須包含下列程式碼行:

<Object name="filter.fcgi">
			Service fn="filter-fastcgi" bind-path="<remote-host>:<remote-port>"
</Object>

如果要篩選位於 fcgi 目錄 (此目錄位於 Web 伺服器實例的文件根目錄下) 下大小為「26868」位元組的檔案「FilterThisFile」,則向「http://localhost/fcgi/filter/FilterThisFile」提出請求時,會產生下列輸出:

This page was Filtered by SimpleFilter FastCGI filter

file size = 26868

stdin size = 0

loop count = 1... so far read 3072 bytes

loop count = 2... so far read 6144 bytes

loop count = 3... so far read 9216 bytes

loop count = 4... so far read 12288 bytes

loop count = 5... so far read 15360 bytes

loop count = 6... so far read 18432 bytes

loop count = 7... so far read 21504 bytes

loop count = 8... so far read 24576 bytes

loop count = 9... so far read 26868 bytes