Nginx HTTP FastCGI 模塊

2020-10-26 18:00 更新

摘 要

這個(gè)模塊允許 Nginx 與 FastCGI 進(jìn)程交互,并通過傳遞參數(shù)來控制 FastCGI 進(jìn)程工作。

配置實(shí)例:

location / {
  fastcgi_pass   localhost:9000;
  fastcgi_index  index.php;

  fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
  fastcgi_param  QUERY_STRING     $query_string;
  fastcgi_param  REQUEST_METHOD   $request_method;
  fastcgi_param  CONTENT_TYPE     $content_type;
  fastcgi_param  CONTENT_LENGTH   $content_length;
}

語法:

fastcgi_buffers

syntax: fastcgi_buffers the_number is_size;

default: fastcgi_buffers 84k/8k;

context: http, server, location

該指令集設(shè)置緩沖區(qū)的數(shù)量和大小,用于緩存從 FastCGI Server 接收到的數(shù)據(jù)。默認(rèn)情況下,一個(gè)緩沖區(qū)的大小相當(dāng)于一個(gè)頁面的大小。根據(jù)平臺(tái)的不同設(shè)置為4K/8K

fastcgi_buffer_size

語法: fastcgi_buffer_size 大小

默認(rèn): fastcgi_buffer_size 4k / 8k

環(huán)境: http, server, location

該命令設(shè)置緩沖區(qū)大小,其中將讀取第一部分的輸出,從 fastcgi 服務(wù)端獲取

在這個(gè)輸出的小輸出標(biāo)頭部分的位置,作為一項(xiàng)規(guī)則

默認(rèn)情況下,緩沖區(qū)大小是等于一個(gè) fastcgi 緩沖區(qū)大小,允許將其設(shè)置為更少

fastcgi_cache

syntax: fastcgi_cache zone;

default: none

context: http, server, location

設(shè)置緩存在共享內(nèi)存中的名稱. 一塊區(qū)域可以被用于不用的地方.

fastcgi_cache_key

syntax: fastcgi_cache_key line ;

default: none

context: http, server, location

設(shè)置緩存的 key , 例:

fastcgi_cache_key localhost: 9000 $ request_uri;

fastcgi_cache_methods

語法: fastcgi_cache_methods [GET HEAD POST];

默認(rèn): fastcgi_cache_methods GET HEAD;

環(huán)境: main, http, location

GET / HEAD 語法糖, 也就是. 即使你剛剛設(shè)置,也無法禁用 GET / HEAD

fastcgi_cache_methods  POST;

fastcgi_cache_min_uses

syntax: fastcgi_cache_min_uses n

default: fastcgi_cache_min_uses 1

context: http, server, location

TODO: Description.

fastcgi_cache_path

syntax: fastcgi_cache_path /path/to/cache [levels=m:n keys_zone=name:time inactive=time clean_time=time]

default: none

context: http, server, location

TODO: Description.

fastcgi_cache_use_stale

syntax: fastcgi_cache_use_stale [updating|error|timeout|invalid_header|http_500]

default: fastcgi_cache_use_stale off;

context: http, server, location

TODO: Description.

fastcgi_cache_valid

syntax: fastcgi_cache_valid [http_error_code|time]

default: none

context: http, server, location

TODO: Description.

fastcgi_index

syntax: fastcgi_index file

default: none

context: http, server, location

The name of the file which will be appended to the URI and stored in the variable $fastcgi_script_name if URI concludes with a slash.

fastcgi_hide_header

syntax: fastcgi_hide_header name

context: http, server, location

默認(rèn)情況下Nginx 不會(huì)從FastCGI 進(jìn)程里給客戶端發(fā)送"Status" 和"X-Accel-..." 消息頭。這個(gè)指令可以用來掩飾別的headers 。

如果需要"Status" 和"X-Accel-..." 消息頭,那就需要使用這個(gè)指令讓FastCGI 強(qiáng)制發(fā)送消息頭給客戶端。

fastcgi_ignore_client_abort

syntax: fastcgi_ignore_client_abort on|off

default: fastcgi_ignore_client_abort off

context: http, server, location

這個(gè)指令用來決定忽略用戶取消的請求。

fastcgi_intercept_errors

syntax: fastcgi_intercept_errors on|off

default: fastcgi_intercept_errors off

context: http, server, location

這個(gè)指令用來決定是否要把客戶端轉(zhuǎn)向4xx和5xx錯(cuò)誤頁,或允許Nginx自動(dòng)指定錯(cuò)誤頁頁。

注意:你需要在此明確錯(cuò)誤頁,它才是有用的。Igor 曾說:“如果沒有定制的處理機(jī)制,Nginx不會(huì)攔截一個(gè)沒有缺省頁的錯(cuò)誤。Nginx 只會(huì)攔截一些小的錯(cuò)誤,放過其他一些。

fastcgi_param

syntax: fastcgi_param parameter value

default: none

context: http, server, location

該指令指定的參數(shù),將被傳遞給FastCGI-server。

它可能使用字符串、變量及其它們的組合來作為參數(shù)值。如果不在此制定參數(shù),它就會(huì)繼承外層設(shè)置;如果在此設(shè)置了參數(shù),將清除外層相關(guān)設(shè)置,僅啟用本層設(shè)置。

下面是一個(gè)例子,對于PHP來說的最精簡的必要參數(shù):

  fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
  fastcgi_param  QUERY_STRING     $query_string;

參數(shù)SCRIPT_FILENAME 是PHP 用來確定執(zhí)行腳本的名字,而參數(shù)QUERY_STRING 是它的一個(gè)子參數(shù)。

如果要處理POST,那么這三個(gè)附加參數(shù)是必要的:

  fastcgi_param  REQUEST_METHOD   $request_method;
  fastcgi_param  CONTENT_TYPE     $content_type;
  fastcgi_param  CONTENT_LENGTH   $content_length;

如果PHP 在編譯時(shí)使用了--enable-force-cgi-redirect選項(xiàng),設(shè)置參數(shù)REDIRECT_STATUS 的值為200就是必須的了。

  fastcgi_param  REDIRECT_STATUS  200;


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號