FastCGI模块(FastCGI)


回目录

·摘要

这个模块允许nginx同FastCGI协同工作,并且控制哪些参数将被安全传递。
例:
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;
}
一个在缓存中的实例:
http {
  fastcgi_cache_path   /path/to/cache  levels=1:2
                       keys_zone=NAME:10m
                       inactive=5m     clean_time=2h00m;
 
  server {
    location / {
      fastcgi_pass    http://127.0.0.1;
      fastcgi_cache   NAME;
      fastcgi_cache_valid   200 302  1h;
      fastcgi_cache_valid   301      1d;
      fastcgi_cache_valid   any      1m;
      fastcgi_cache_min_uses  1;
      fastcgi_cache_use_stale error  timeout invalid_header http_500;
    }
  }
}
0.7.48以后,缓存遵循后端服务器的Cache-Control, Expires等。

·指令

fastcgi_buffers

语法:fastcgi_buffers the_number is_size;
默认值:fastcgi_buffers 8 4k/8k;
使用字段:http, server, location
这个参数指定了从FastCGI服务器到来的应答,本地将用多少和多大的缓冲区读取,默认这个参数等于分页大小,根据环境的不同可能是4K, 8K或16K。

fastcgi_buffer_size

语法:fastcgi_buffer_size the_size ;
默认值:fastcgi_buffer_size 4k/8k ;
使用字段:http, server, location
这个参数指定将用多大的缓冲区来读取从FastCGI服务器到来应答的第一部分。
通常来说在这个部分中包含一个小的应答头。
默认这个值为fastcgi_buffers指令中的每块大小,可以将这个值设置更小。

fastcgi_cache

语法:fastcgi_cache zone;
默认值:off
使用字段:http, server, location
为缓存实际使用的共享内存指定一个区域,相同的区域可以用在不同的地方。

fastcgi_cache_key

语法:fastcgi_cache_key line ;
默认值:none
使用字段:http, server, location
设置缓存的关键字,如:
fastcgi_cache_key localhost: 9000 $ request_uri;

fastcgi_cache_methods

语法:fastcgi_cache_methods [GET HEAD POST];
默认值:fastcgi_cache_methods GET HEAD;
使用字段:main,http,location
无法禁用GET/HEAD ,即使你只是这样设置:
fastcgi_cache_methods  POST;

fastcgi_cache_min_uses

语法:fastcgi_cache_min_uses n
默认值:fastcgi_cache_min_uses 1
使用字段:http, server, location
未知。

fastcgi_cache_path

语法:fastcgi_cache_path /path/to/cache [levels=m:n keys_zone=name:time inactive=time clean_time=time]
默认值:none
使用字段:http, server, location
未知。

fastcgi_cache_use_stale

语法:fastcgi_cache_use_stale [updating|error|timeout|invalid_header|http_500]
默认值:fastcgi_cache_use_stale off;
使用字段:http, server, location
未知。

fastcgi_cache_valid

语法:fastcgi_cache_valid [http_error_code|time]
默认值:none
使用字段:http, server, location
未知。

fastcgi_index

语法:fastcgi_index file
默认值:none
使用字段:http, server, location
如果URI以斜线结尾,参数将加到URI后面,这个值将存储在变量$fastcgi_script_name中。

fastcgi_hide_header

语法:fastcgi_hide_header name
使用字段:http, server, location
默认情况下nginx不会将来自FastCGI服务器的"Status"和"X-Accel-..."头传送到客户端,这个参数也可以隐藏某些其它的头。
如果必须传递"Status"和"X-Accel-..."头,则必须使用fastcgi_pass_header强制其传送到客户端。

fastcgi_ignore_client_abort

语法:fastcgi_ignore_client_abort on|off
默认值:fastcgi_ignore_client_abort off
使用字段:http, server, location
如果当前连接请求FastCGI服务器失败,为防止其与nginx服务器断开连接,可以用这个指令。

fastcgi_intercept_errors

语法:fastcgi_intercept_errors on|off
默认值:fastcgi_intercept_errors off
使用字段:http, server, location
这个指令指定是否传递4xx和5xx错误信息到客户端,或者允许nginx使用error_page处理错误信息。
你必须明确的在error_page中指定处理方法使这个参数有效,正如Igor所说“如果没有适当的处理方法,nginx不会拦截一个错误,这个错误不会显示自己的默认页面,这里允许通过某些方法拦截错误。

fastcgi_max_temp_file_size

语法:fastcgi_max_temp_file_size 0
默认值:?
使用字段:?
根据源代码关闭FastCGI缓冲。

fastcgi_param

语法:fastcgi_param parameter value
默认值:none
使用字段:http, server, location
指定一些传递到FastCGI服务器的参数。
可以使用字符串,变量,或者其组合,这里的设置不会继承到其他的字段,设置在当前字段会清除掉任何之前的定义。
下面是一个PHP需要使用的最少参数:
  fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
  fastcgi_param  QUERY_STRING     $query_string;
PHP使用SCRIPT_FILENAME参数决定需要执行哪个脚本,QUERY_STRING包含请求中的某些参数。
如果要处理POST请求,则需要另外增加三个参数:
  fastcgi_param  REQUEST_METHOD   $request_method;
  fastcgi_param  CONTENT_TYPE     $content_type;
  fastcgi_param  CONTENT_LENGTH   $content_length;
如果PHP在编译时带有--enable-force-cgi-redirect,则必须传递值为200的REDIRECT_STATUS参数:
fastcgi_param  REDIRECT_STATUS  200;

fastcgi_pass

语法:fastcgi_pass fastcgi-server
默认值:none
使用字段:http, server, location
指定FastCGI服务器监听端口与地址,可以是本机或者其它:
fastcgi_pass   localhost:9000;
使用Unix socket:
fastcgi_pass   unix:/tmp/fastcgi.socket;
同样可以使用一个upstream字段名称:
upstream backend  {
  server   localhost:1234;
}
 
fastcgi_pass   backend;

fastcgi_pass_header

语法:fastcgi_pass_header name
默认值:none
使用字段:http, server, location

fastcgi_read_timeout

语法:fastcgi_read_timeout time
默认值:60
使用字段:http, server, location
前端FastCGI服务器的响应超时时间,如果有一些直到它们运行完才有输出的长时间运行的FastCGI进程,或者在错误日志中出现前端服务器响应超时错误,可能需要调整这个值。

fastcgi_redirect_errors

语法:fastcgi_redirect_errors on|off
将来自fastcgi_intercept_errors的错误重命名。参数传递到FastCGI服务器。
请求头以参数的形式传递到FastCGI服务器,在FastCGI的应用程序或者脚本中运行,这些参数通常是环境变量的形式,例如:User-agent头是以HTTP_USER_AGENT形式转发,另外可以借助fastcgi_param指令可以传递任意参数的HTTP请求头。

fastcgi_split_path_info

语法:fastcgi_split_path_info regex
使用字段:location
可用版本:0.7.31以上
location ~ ^(.+\.php)(.*)$ {
...
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
...
}

fastcgi_store

语法:fastcgi_store [on | off | path]
默认值:fastcgi_store off
使用字段:http, server, location
制定了存储前端文件的路径,参数on指定了将使用root和alias指令相同的路径,off禁止存储,此外,参数中可以使用变量使路径名更明确:
fastcgi_store   /data/www$original_uri;
应答中的"Last-Modified"头将设置文件的最后修改时间,为了使这些文件更加安全,可以将其在一个目录中存为临时文件,使用fastcgi_temp_path指令。
这个指令可以用在为那些不是经常改变的后端动态输出创建本地拷贝的过程中。如:
location /images/ {
  root                 /data/www;
  error_page           404 = /fetch$uri;
}
 
location /fetch {
  internal;
 
  fastcgi_pass           fastcgi://backend;
  fastcgi_store          on;
  fastcgi_store_access   user:rw  group:rw  all:r;
  fastcgi_temp_path      /data/temp;
 
  alias                  /data/www;
}
fastcgi_store并不是缓存,某些需求下它更像是一个镜像。

fastcgi_store_access

语法:fastcgi_store_access users:permissions [users:permission ...]
默认值:fastcgi_store_access user:rw
使用字段:http, server, location
这个参数指定创建文件或目录的权限,例如:
fastcgi_store_access  user:rw  group:rw  all:r;
如果要指定一个组的人的相关权限,可以不写用户,如:
fastcgi_store_access  group:rw  all:r;

·变量

$fastcgi_script_name

这个变量等于一个以斜线结尾的请求URI加上fastcgi_index给定的参数。可以用这个变量代替SCRIPT_FILENAME 和PATH_TRANSLATED,以确定php脚本的名称。
如下例,请求"/info/":
 fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  /home/www/scripts/php$fastcgi_script_name;
SCRIPT_FILENAME等于"/home/www/scripts/php/info/index.php"。

·参考文档

Original Documentation
Nginx Http FastCGI Module

前进->Geo模块(Geo)