On this page
Module ngx_http_fastcgi_module
- Example Configuration
- Directives
- fastcgi_bind
- fastcgi_buffer_size
- fastcgi_buffering
- fastcgi_buffers
- fastcgi_busy_buffers_size
- fastcgi_cache
- fastcgi_cache_background_update
- fastcgi_cache_bypass
- fastcgi_cache_key
- fastcgi_cache_lock
- fastcgi_cache_lock_age
- fastcgi_cache_lock_timeout
- fastcgi_cache_max_range_offset
- fastcgi_cache_methods
- fastcgi_cache_min_uses
- fastcgi_cache_path
- fastcgi_cache_purge
- fastcgi_cache_revalidate
- fastcgi_cache_use_stale
- fastcgi_cache_valid
- fastcgi_catch_stderr
- fastcgi_connect_timeout
- fastcgi_force_ranges
- fastcgi_hide_header
- fastcgi_ignore_client_abort
- fastcgi_ignore_headers
- fastcgi_index
- fastcgi_intercept_errors
- fastcgi_keep_conn
- fastcgi_limit_rate
- fastcgi_max_temp_file_size
- fastcgi_next_upstream
- fastcgi_next_upstream_timeout
- fastcgi_next_upstream_tries
- fastcgi_no_cache
- fastcgi_param
- fastcgi_pass
- fastcgi_pass_header
- fastcgi_pass_request_body
- fastcgi_pass_request_headers
- fastcgi_read_timeout
- fastcgi_request_buffering
- fastcgi_send_lowat
- fastcgi_send_timeout
- fastcgi_socket_keepalive
- fastcgi_split_path_info
- fastcgi_store
- fastcgi_store_access
- fastcgi_temp_file_write_size
- fastcgi_temp_path
- 传递给 FastCGI 服务器的参数
- Embedded Variables
ngx_http_fastcgi_module
模块允许将请求传递到 FastCGI 服务器。
Example Configuration
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;
}
Directives
Syntax: | fastcgi_bind address [transparent] | off; |
Default: | — |
Context: | http , server , location |
该指令出现在版本 0.8.22 中。
与 FastCGI 服务器的传出连接源自具有可选端口(1.11.2)的指定本地 IP 地址。参数值可以包含变量(1.3.12)。特殊值off
(1.3.12)取消了从以前的配置级别继承的fastcgi_bind
指令的效果,这使系统可以自动分配本地 IP 地址和端口。
transparent
参数(1.11.0)允许与 FastCGI 服务器的传出连接源自 nonlocalIP 地址,例如,源自客户端的真实 IP 地址:
fastcgi_bind $remote_addr transparent;
为了使此参数起作用,通常必须使用superuser特权运行 nginx worker 进程。在 Linux 上,不需要(1.13.8),就好像指定了transparent
参数一样,辅助进程从主进程继承CAP_NET_RAW
功能。还必须配置内核路由表以拦截来自 FastCGI 服务器的网络流量。
Syntax: | fastcgi_buffer_size size; |
Default: | fastcgi_buffer_size 4k|8k; |
Context: | http , server , location |
设置用于读取从 FastCGI 服务器接收到的响应的第一部分的缓冲区的size
。这部分通常包含一个小的响应头。默认情况下,缓冲区大小等于一个内存页。根据平台的不同,它可以是 4K 或 8K。但是,它可以做得更小。
Syntax: | fastcgi_buffering on | off; |
Default: | fastcgi_buffering on; |
Context: | http , server , location |
该指令出现在 1.5.6 版中。
启用或禁用来自 FastCGI 服务器的响应缓冲。
启用缓冲后,nginx 会尽快从 FastCGI 服务器接收响应,并将其保存到fastcgi_buffer_size和fastcgi_buffers指令设置的缓冲区中。如果整个响应都无法容纳到内存中,则可以将一部分响应保存到磁盘上的temporary file。写入临时文件由fastcgi_max_temp_file_size和fastcgi_temp_file_write_size指令控制。
禁用缓冲后,响应一收到就立即同步传递给客户端。 nginx 不会尝试从 FastCGI 服务器读取整个响应。 fastcgi_buffer_size指令设置了 nginx 一次可以从服务器接收的最大数据大小。
也可以通过在“ X-Accel-Buffering”响应标题字段中传递“ yes
”或“ no
”来启用或禁用缓冲。可以使用fastcgi_ignore_headers指令禁用此功能。
Syntax: | fastcgi_buffers number size; |
Default: | fastcgi_buffers 8 4k|8k; |
Context: | http , server , location |
为单个连接设置用于从 FastCGI 服务器读取响应的缓冲区的number
和size
。默认情况下,缓冲区大小等于一个内存页。根据平台的不同,它可以是 4K 或 8K。
Syntax: | fastcgi_busy_buffers_size size; |
Default: | fastcgi_busy_buffers_size 8k|16k; |
Context: | http , server , location |
启用来自 FastCGI 服务器的buffering响应后,将限制在忙于向客户端发送响应而尚未完全读取响应的缓冲区总数size
。同时,其余的缓冲区可用于读取响应,并在需要时将响应的一部分缓冲到临时文件中。默认情况下,size
受fastcgi_buffer_size和fastcgi_buffers指令设置的两个缓冲区的大小限制。
Syntax: | fastcgi_cache zone | off; |
Default: | fastcgi_cache off; |
Context: | http , server , location |
定义用于缓存的共享内存区域。同一区域可以在多个地方使用。参数值可以包含变量(1.7.9)。 off
参数禁用从先前配置级别继承的缓存。
Syntax: | fastcgi_cache_background_update on | off; |
Default: | fastcgi_cache_background_update off; |
Context: | http , server , location |
该指令出现在 1.11.10 版本中。
允许启动后台子请求以更新过期的缓存项,同时将陈旧的缓存响应返回给客户端。请注意,在更新过时的缓存响应时,有必要allow使用。
Syntax: | fastcgi_cache_bypass string ...; |
Default: | — |
Context: | http , server , location |
定义不从缓存中获取响应的条件。如果字符串参数的至少一个值不为空且不等于“ 0”,那么将不会从缓存中获取响应:
fastcgi_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
fastcgi_cache_bypass $http_pragma $http_authorization;
可以与fastcgi_no_cache指令一起使用。
Syntax: | fastcgi_cache_key string; |
Default: | — |
Context: | http , server , location |
定义用于缓存的密钥,例如
fastcgi_cache_key localhost:9000$request_uri;
Syntax: | fastcgi_cache_lock on | off; |
Default: | fastcgi_cache_lock off; |
Context: | http , server , location |
该指令出现在版本 1.1.12 中。
启用后,一次只允许一个请求通过将请求传递到 FastCGI 服务器来填充根据fastcgi_cache_key指令标识的新缓存元素。在fastcgi_cache_lock_timeout指令设置的时间之前,同一缓存元素的其他请求将 await 响应出现在缓存中,或者 await 该元素被释放的缓存锁。
Syntax: | fastcgi_cache_lock_age time; |
Default: | fastcgi_cache_lock_age 5s; |
Context: | http , server , location |
该指令出现在版本 1.7.8 中。
如果对于指定的time
,传递到 FastCGI 服务器的最后一个用于填充新缓存元素的请求尚未完成,则可以将另一个请求传递给 FastCGI 服务器。
Syntax: | fastcgi_cache_lock_timeout time; |
Default: | fastcgi_cache_lock_timeout 5s; |
Context: | http , server , location |
该指令出现在版本 1.1.12 中。
为fastcgi_cache_lock设置超时。当time
过期时,请求将被传递到 FastCGI 服务器,但是,响应将不会被缓存。
Note
在 1.7.8 之前,可以缓存响应。
Syntax: | fastcgi_cache_max_range_offset number; |
Default: | — |
Context: | http , server , location |
该指令出现在 1.11.6 版本中。
设置字节范围请求的字节偏移量。如果范围超出偏移量,则范围请求将传递到 FastCGI 服务器,并且不会缓存响应。
Syntax: | fastcgi_cache_methods GET | HEAD | POST ...; |
Default: | fastcgi_cache_methods GET HEAD; |
Context: | http , server , location |
该指令出现在版本 0.7.59 中。
如果此伪指令中列出了客户端请求方法,则将缓存响应。尽管建议显式指定“ GET
”和“ HEAD
”方法,但始终将它们添加到列表中。另请参见fastcgi_no_cache指令。
Syntax: | fastcgi_cache_min_uses number; |
Default: | fastcgi_cache_min_uses 1; |
Context: | http , server , location |
设置number
请求,之后将缓存响应。
Syntax: | fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; |
Default: | — |
Context: | http |
设置缓存的路径和其他参数。缓存数据存储在文件中。缓存中的键名和文件名都是将 MD5 功能应用于代理 URL 的结果。 levels
参数定义缓存的层次结构级别:从 1 到 3,每个级别接受值 1 或 2.例如,在以下配置中
fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
缓存中的文件名如下所示:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
首先将缓存的响应写入临时文件,然后重命名该文件。从 0.8.9 版本开始,可以将临时文件和缓存放在不同的文件系统上。但是,请注意,在这种情况下,文件是跨两个文件系统复制的,而不是廉价的重命名操作。因此,建议对于任何给定位置,将高速缓存和保存临时文件的目录都放在同一文件系统上。基于use_temp_path
参数(1.7.10)设置临时文件的目录。如果省略此参数或将其设置为值on
,则将使用fastcgi_temp_path指令为给定位置设置的目录。如果该值设置为off
,则临时文件将直接放置在缓存目录中。
此外,所有活动键和有关数据的信息都存储在共享存储区中,该存储区的name
和size
由keys_zone
参数配置。一个 1 MB 的区域可以存储大约 8000 个密钥。
Note
作为commercial subscription的一部分,共享内存区域还存储扩展的缓存information,因此,需要为相同数量的键指定更大的区域大小。例如,一个 1 MB 的区域可以存储大约 4000 个密钥。
在inactive
参数指定的时间内未访问的缓存数据将被从缓存中删除,无论它们是否新鲜。默认情况下,inactive
设置为 10 分钟。
特殊的“高速缓存管理器”过程监视具有高速缓存的文件系统上max_size
参数设置的最大高速缓存大小和min_free
(1.19.1)参数设置的最小可用空间。当超出大小或没有足够的可用空间时,它将删除最近最少使用的数据。在由manager_files
,manager_threshold
和manager_sleep
参数(1.11.5)配置的迭代中删除数据。在一次迭代中,最多删除manager_files
个项目(默认为 100)。一次迭代的持续时间受manager_threshold
参数的限制(默认情况下为 200 毫秒)。在迭代之间,由manager_sleep
参数配置的暂停(默认为 50 毫秒)。
启动一分钟后,特殊的“缓存加载器”过程被激活。它将有关存储在文件系统上的先前缓存的数据的信息加载到缓存区域中。加载也以迭代方式完成。在一次迭代中,最多加载loader_files
个项目(默认情况下为 100)。此外,一次迭代的持续时间受到loader_threshold
参数的限制(默认为 200 毫秒)。在迭代之间,由loader_sleep
参数配置的暂停(默认为 50 毫秒)。
此外,以下参数是我们的commercial subscription的一部分:
purger =开|关
- 指示是否将由缓存清除器(1.7.12)从磁盘上删除与wildcard key匹配的缓存条目。将参数设置为
on
(默认值为off
)将激活“缓存清除器”过程,该过程将永久性地遍历所有缓存条目,并删除与通配符匹配的条目。
purger_files= number
- 设置一次迭代(1.7.12)期间要扫描的项目数。默认情况下,
purger_files
设置为 10.
purger_threshold= number
- 设置一次迭代的持续时间(1.7.12)。默认情况下,
purger_threshold
设置为 50 毫秒。
purger_sleep= number
- 设置迭代之间的暂停(1.7.12)。默认情况下,
purger_sleep
设置为 50 毫秒。
Note
在版本 1.7.3、1.7.7 和 1.11.10 中,缓存头格式已更改。升级到较新的 nginx 版本后,以前缓存的响应将被视为无效。
Syntax: | fastcgi_cache_purge string ...; |
Default: | — |
Context: | http , server , location |
该指令出现在 1.5.7 版中。
定义将请求视为高速缓存清除请求的条件。如果字符串参数的至少一个值不为空且不等于“ 0”,则将删除具有相应cache key的缓存条目。通过返回 204(无内容)响应来指示成功操作的结果。
如果清除请求的cache key以星号(“ *
”)结尾,则所有与通配符匹配的高速缓存条目都将从高速缓存中删除。但是,这些条目将保留在磁盘上,直到它们被删除为inactivity或被cache purger(1.7.12)处理或客户端尝试访问它们为止。
Example configuration:
fastcgi_cache_path /data/nginx/cache keys_zone=cache_zone:10m;
map $request_method $purge_method {
PURGE 1;
default 0;
}
server {
...
location / {
fastcgi_pass backend;
fastcgi_cache cache_zone;
fastcgi_cache_key $uri;
fastcgi_cache_purge $purge_method;
}
}
Note
此功能是我们commercial subscription的一部分。
Syntax: | fastcgi_cache_revalidate on | off; |
Default: | fastcgi_cache_revalidate off; |
Context: | http , server , location |
该指令出现在 1.5.7 版中。
使用带有“ If-Modified-Since”和“ If-None-Match”头域的条件请求,启用过期缓存项的重新验证。
Syntax: | fastcgi_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_503 | http_403 | http_404 | http_429 | off ...; |
Default: | fastcgi_cache_use_stale off; |
Context: | http , server , location |
确定在与 FastCGI 服务器通信期间发生错误时,在什么情况下可以使用过时的缓存响应。该指令的参数与fastcgi_next_upstream指令的参数匹配。
如果无法选择 FastCGI 服务器来处理请求,则error
参数还允许使用陈旧的缓存响应。
另外,如果当前正在更新,则updating
参数允许使用陈旧的缓存响应。这样可以在更新缓存的数据时最大程度地减少对 FastCGI 服务器的访问次数。
在响应过时(1.11.10)后,还可以在响应 Headers 中直接启用使用过时的缓存响应达指定的秒数。这比使用指令参数的优先级低。
如果当前正在更新“ Cache_Control”头字段的“ stale-while-revalidate”extensions,则允许使用陈旧的缓存响应。
“ Cache-Control”Headers 字段的“ stale-if-error”extensions 允许在发生错误的情况下使用过期的缓存响应。
为了在填充新的缓存元素时最大程度地减少对 FastCGI 服务器的访问次数,可以使用fastcgi_cache_lock指令。
Syntax: | fastcgi_cache_valid [code ...] time; |
Default: | — |
Context: | http , server , location |
设置不同响应代码的缓存时间。例如,以下指令
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404 1m;
为代码 200 和 302 的响应设置 10 分钟的缓存,为代码 404 的响应设置 1 分钟的缓存。
如果仅指定缓存time
fastcgi_cache_valid 5m;
那么仅缓存 200、301 和 302 响应。
另外,可以指定any
参数来缓存任何响应:
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
缓存参数也可以直接在响应头中设置。这比使用伪指令设置缓存时间具有更高的优先级。
“ X-Accel-Expires”Headers 字段以秒为单位设置响应的缓存时间。零值禁用缓存响应。如果该值以
@
前缀开头,则它设置自 Epoch 以来的绝对时间(以秒为单位),可以在该绝对时间之前缓存响应。如果标题不包括“ X-Accel-Expires”字段,则可以在标题字段“ Expires”或“ Cache-Control”中设置缓存参数。
如果 Headers 包含“ Set-Cookie”字段,则不会缓存此类响应。
如果 Headers 包含具有特殊值“
*
”的“ Vary”字段,则不会缓存此类响应(1.7.7)。如果 Headers 包含带有另一个值的“ Vary”字段,则将考虑相应的请求 Headers 字段(1.7.7)来缓存此类响应。
可以使用fastcgi_ignore_headers指令禁用对这些响应头字段中的一个或多个的处理。
Syntax: | fastcgi_catch_stderr string; |
Default: | — |
Context: | http , server , location |
设置一个字符串,以在从 FastCGI 服务器接收到的响应的错误流中进行搜索。如果找到string
,则认为 FastCGI 服务器已返回invalid response。这样可以处理 nginx 中的应用程序错误,例如:
location /php/ {
fastcgi_pass backend:9000;
...
fastcgi_catch_stderr "PHP Fatal error";
fastcgi_next_upstream error timeout invalid_header;
}
Syntax: | fastcgi_connect_timeout time; |
Default: | fastcgi_connect_timeout 60s; |
Context: | http , server , location |
定义用于与 FastCGI 服务器构建连接的超时。请注意,此超时通常不能超过 75 秒。
Syntax: | fastcgi_force_ranges on | off; |
Default: | fastcgi_force_ranges off; |
Context: | http , server , location |
该指令出现在版本 1.7.7 中。
对来自 FastCGI 服务器的缓存和未缓存响应均启用字节范围支持,而不管这些响应中的“接受范围”字段如何。
Syntax: | fastcgi_hide_header field; |
Default: | — |
Context: | http , server , location |
默认情况下,nginx 不会将 FastCGI 服务器的响应中的 Headers 字段“状态”和“ X-Accel -...”传递给客户端。 fastcgi_hide_header
指令设置了不会传递的其他字段。相反,如果需要允许传递字段,则可以使用fastcgi_pass_header指令。
Syntax: | fastcgi_ignore_client_abort on | off; |
Default: | fastcgi_ignore_client_abort off; |
Context: | http , server , location |
确定当客户端关闭连接而不 await 响应时是否关闭与 FastCGI 服务器的连接。
Syntax: | fastcgi_ignore_headers field ...; |
Default: | — |
Context: | http , server , location |
禁用 FastCGI 服务器中某些响应 Headers 字段的处理。可以忽略以下字段:“ X-Accel-Redirect”,“ X-Accel-Expires”,“ X-Accel-Limit-Rate”(1.1.6),“ X-Accel-Buffering”(1.1.6) ,“ X-Accel-Charset”(1.1.6),“ Expires”,“ Cache-Control”,“ Set-Cookie”(0.8.44)和“ Vary”(1.7.7)。
如果未禁用,则处理这些头字段具有以下效果:
“ X-Accel-Expires”,“ Expires”,“ Cache-Control”,“ Set-Cookie”和“ Vary”设置响应caching的参数;
“ X-Accel-Redirect”对指定的 URI 执行internal redirect;
“ X-Accel-Limit-Rate”设置rate limit用于将响应传输到客户端;
“ X-Accel-Buffering”启用或禁用响应buffering;
“ X-Accel-Charset”设置所需的响应charset。
Syntax: | fastcgi_index name; |
Default: | — |
Context: | http , server , location |
设置文件名,该文件名将以$fastcgi_script_name
变量的值附加在以斜杠结尾的 URI 之后。例如,使用这些设置
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
和“ /page.php
”请求,则SCRIPT_FILENAME
参数将等于“ /home/www/scripts/php/page.php
”,对于“ /
”请求,它将等于“ /home/www/scripts/php/index.php
”。
Syntax: | fastcgi_intercept_errors on | off; |
Default: | fastcgi_intercept_errors off; |
Context: | http , server , location |
确定代码大于或等于 300 的 FastCGI 服务器响应是应该传递给客户端还是被拦截并重定向到 nginx 以使用error_page指令进行处理。
Syntax: | fastcgi_keep_conn on | off; |
Default: | fastcgi_keep_conn off; |
Context: | http , server , location |
该指令出现在版本 1.1.4 中。
默认情况下,FastCGI 服务器将在发送响应后立即关闭连接。但是,当此伪指令设置为值on
时,nginx 将指示 FastCGI 服务器保持连接打开。尤其是要使与 FastCGI 服务器的keepalive连接正常运行,这是必需的。
Syntax: | fastcgi_limit_rate rate; |
Default: | fastcgi_limit_rate 0; |
Context: | http , server , location |
该指令出现在版本 1.7.7 中。
限制从 FastCGI 服务器读取响应的速度。 rate
以每秒字节数指定。零值禁用速率限制。该限制是根据请求设置的,因此,如果 nginx 同时打开两个与 FastCFI 服务器的连接,则总速率将是指定限制的两倍。仅当启用buffering来自 FastCGI 服务器的响应时,此限制才有效。
Syntax: | fastcgi_max_temp_file_size size; |
Default: | fastcgi_max_temp_file_size 1024m; |
Context: | http , server , location |
启用来自 FastCGI 服务器的buffering响应,并且整个响应不适合fastcgi_buffer_size和fastcgi_buffers指令设置的缓冲区时,可以将一部分响应保存到临时文件中。此伪指令设置临时文件的最大size
。一次写入fastcgi_temp_file_write_size指令设置写入临时文件的数据大小。
零值禁用对临时文件的响应的缓冲。
Syntax: | fastcgi_next_upstream error | timeout | invalid_header | http_500 | http_503 | http_403 | http_404 | http_429 | non_idempotent | off ...; |
Default: | fastcgi_next_upstream error timeout; |
Context: | http , server , location |
指定在哪种情况下将请求传递到下一个服务器:
error
- 与服务器构建连接,向服务器传递请求或读取响应头时发生错误;
timeout
- 与服务器构建连接,向服务器传递请求或读取响应 Headers 时发生超时;
invalid_header
- 服务器返回了空的或无效的响应;
http_500
- 服务器返回响应,代码为 500;
http_503
- 服务器返回响应,代码为 503;
http_403
- 服务器返回响应,代码为 403;
http_404
- 服务器返回了代码为 404 的响应;
http_429
- 服务器返回响应,代码为 429(1.11.13);
non_idempotent
通常,如果请求已发送到上游服务器(1.9.13),则使用non-idempotent方法(
POST
,LOCK
,PATCH
)的请求不会传递到下一个服务器。明确启用此选项将允许重试此类请求;off
- 禁用将请求传递到下一个服务器。
应该记住的是,只有在还没有任何内容发送给客户端的情况下,才有可能将请求传递给下一台服务器。即,如果在响应的传输过程中发生错误或超时,则无法解决该问题。
该指令还定义了与服务器通信的unsuccessful attempt。 error
,timeout
和invalid_header
的情况始终被认为是失败的尝试,即使未在指令中指定它们也是如此。 http_500
,http_503
和http_429
的情况只有在指令中指定时才被认为是失败的尝试。决不会将http_403
和http_404
的情况视为不成功的尝试。
Syntax: | fastcgi_next_upstream_timeout time; |
Default: | fastcgi_next_upstream_timeout 0; |
Context: | http , server , location |
该指令出现在版本 1.7.5 中。
限制可以将请求传递到next server的时间。 0
值关闭了此限制。
Syntax: | fastcgi_next_upstream_tries number; |
Default: | fastcgi_next_upstream_tries 0; |
Context: | http , server , location |
该指令出现在版本 1.7.5 中。
限制将请求传递到next server的可能尝试次数。 0
值关闭了此限制。
Syntax: | fastcgi_no_cache string ...; |
Default: | — |
Context: | http , server , location |
定义不将响应保存到缓存的条件。如果字符串参数的至少一个值不为空且不等于“ 0”,则将不保存响应:
fastcgi_no_cache $cookie_nocache $arg_nocache$arg_comment;
fastcgi_no_cache $http_pragma $http_authorization;
可以与fastcgi_cache_bypass指令一起使用。
Syntax: | fastcgi_param parameter value [if_not_empty]; |
Default: | — |
Context: | http , server , location |
设置一个parameter
,该parameter
应该传递给 FastCGI 服务器。 value
可以包含文本,变量及其组合。当且仅当当前级别上未定义fastcgi_param
指令时,这些指令才从上一级继承。
以下示例显示了 PHP 所需的最低设置:
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
SCRIPT_FILENAME
参数在 PHP 中用于确定脚本名称,而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
配置参数构建的,则REDIRECT_STATUS
参数也应使用值“ 200”传递:
fastcgi_param REDIRECT_STATUS 200;
如果指令是用if_not_empty
(1.1.11)指定的,则仅当其值不为空时,此类参数才会传递给服务器:
fastcgi_param HTTPS $https if_not_empty;
Syntax: | fastcgi_pass address; |
Default: | — |
Context: | location , if in location |
设置 FastCGI 服务器的地址。该地址可以指定为域名或 IP 地址以及端口:
fastcgi_pass localhost:9000;
或作为 UNIX 域套接字路径:
fastcgi_pass unix:/tmp/fastcgi.socket;
如果一个域名解析为多个地址,则所有这些地址都将以循环方式使用。另外,可以将地址指定为server group。
参数值可以包含变量。在这种情况下,如果将地址指定为域名,则在描述的server groups中搜索该名称,如果找不到,则使用resolver确定。
Syntax: | fastcgi_pass_header field; |
Default: | — |
Context: | http , server , location |
允许将otherwise disabledHeaders 字段从 FastCGI 服务器传递到客户端。
Syntax: | fastcgi_pass_request_body on | off; |
Default: | fastcgi_pass_request_body on; |
Context: | http , server , location |
指示是否将原始请求正文传递到 FastCGI 服务器。另请参见fastcgi_pass_request_headers指令。
Syntax: | fastcgi_pass_request_headers on | off; |
Default: | fastcgi_pass_request_headers on; |
Context: | http , server , location |
指示是否将原始请求的 Headers 字段传递到 FastCGI 服务器。另请参见fastcgi_pass_request_body指令。
Syntax: | fastcgi_read_timeout time; |
Default: | fastcgi_read_timeout 60s; |
Context: | http , server , location |
定义用于从 FastCGI 服务器读取响应的超时。超时仅在两次连续的读取操作之间设置,而不用于传输整个响应。如果 FastCGI 服务器在此时间内未传输任何内容,则连接将关闭。
Syntax: | fastcgi_request_buffering on | off; |
Default: | fastcgi_request_buffering on; |
Context: | http , server , location |
该指令出现在 1.7.11 版本中。
启用或禁用客户端请求正文的缓冲。
启用缓冲后,在将请求发送到 FastCGI 服务器之前,整个请求正文都是来自客户端的read。
禁用缓冲后,请求主体将在收到请求后立即发送到 FastCGI 服务器。在这种情况下,如果 nginx 已经开始发送请求正文,则无法将请求传递给next server。
Syntax: | fastcgi_send_lowat size; |
Default: | fastcgi_send_lowat 0; |
Context: | http , server , location |
如果指令设置为非零值,nginx 将尝试通过使用kqueue方法的NOTE_LOWAT
标志或SO_SNDLOWAT
套接字选项以及指定的size
来最大程度地减少与 FastCGI 服务器的传出连接上的发送操作数量。
在 Linux,Solaris 和 Windows 上,此伪指令将被忽略。
Syntax: | fastcgi_send_timeout time; |
Default: | fastcgi_send_timeout 60s; |
Context: | http , server , location |
设置将请求传输到 FastCGI 服务器的超时。超时仅在两个连续的写操作之间设置,而不用于整个请求的传输。如果 FastCGI 服务器在此时间内未收到任何信息,则连接将关闭。
Syntax: | fastcgi_socket_keepalive on | off; |
Default: | fastcgi_socket_keepalive off; |
Context: | http , server , location |
该指令出现在 1.15.6 版中。
为与 FastCGI 服务器的传出连接配置“ TCP keepalive”行为。默认情况下,os 的设置对套接字有效。如果伪指令设置为值“ on
”,则将为套接字打开SO_KEEPALIVE
套接字选项。
Syntax: | fastcgi_split_path_info regex; |
Default: | — |
Context: | location |
定义一个正则表达式,以捕获$fastcgi_path_info
变量的值。正则表达式应具有两个捕获:第一个捕获成为$fastcgi_script_name
变量的值,第二个捕获成为$fastcgi_path_info
变量的值。例如,使用这些设置
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
和“ /show.php/article/0001
”请求,则SCRIPT_FILENAME
参数将等于“ /path/to/php/show.php
”,而PATH_INFO
参数将等于“ /article/0001
”。
Syntax: | fastcgi_store on | off | string; |
Default: | fastcgi_store off; |
Context: | http , server , location |
允许将文件保存到磁盘。 on
参数保存具有与指令alias或root相对应的路径的文件。 off
参数禁用文件保存。另外,可以使用带有变量的string
显式设置文件名:
fastcgi_store /data/www$original_uri;
根据收到的“ Last-Modified”响应头字段设置文件的修改时间。首先将响应写入临时文件,然后重命名该文件。从 0.8.9 版本开始,可以将临时文件和永久存储放在不同的文件系统上。但是,请注意,在这种情况下,文件是跨两个文件系统复制的,而不是廉价的重命名操作。因此,建议将对于任何给定位置的已保存文件和由fastcgi_temp_path指令设置的保存临时文件的目录都放在同一文件系统上。
该指令可用于创建静态不可更改文件的本地副本,例如:
location /images/ {
root /data/www;
error_page 404 = /fetch$uri;
}
location /fetch/ {
internal;
fastcgi_pass backend:9000;
...
fastcgi_store on;
fastcgi_store_access user:rw group:rw all:r;
fastcgi_temp_path /data/temp;
alias /data/www/;
}
Syntax: | fastcgi_store_access users:permissions ...; |
Default: | fastcgi_store_access user:rw; |
Context: | http , server , location |
设置新创建的文件和目录的访问权限,例如:
fastcgi_store_access user:rw group:rw all:r;
如果指定了任何group
或all
访问权限,则可以省略user
权限:
fastcgi_store_access group:rw all:r;
Syntax: | fastcgi_temp_file_write_size size; |
Default: | fastcgi_temp_file_write_size 8k|16k; |
Context: | http , server , location |
当启用了从 FastCGI 服务器到临时文件的响应缓冲时,一次限制写入临时文件的数据size
。默认情况下,size
受fastcgi_buffer_size和fastcgi_buffers伪指令设置的两个缓冲区的限制。临时文件的最大大小由fastcgi_max_temp_file_size指令设置。
Syntax: | fastcgi_temp_path path [level1 [level2 [level3]]]; |
Default: | fastcgi_temp_path fastcgi_temp; |
Context: | http , server , location |
定义一个目录,用于存储包含从 FastCGI 服务器接收到的数据的临时文件。在指定目录下最多可以使用三级子目录层次结构。例如,在以下配置中
fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;
临时文件可能如下所示:
/spool/nginx/fastcgi_temp/7/45/00000123457
另请参见fastcgi_cache_path指令的use_temp_path
参数。
传递给 FastCGI 服务器的参数
HTTP 请求 Headers 字段作为参数传递到 FastCGI 服务器。在作为 FastCGI 服务器运行的应用程序和脚本中,这些参数通常可以用作环境变量。例如,“ User-Agent”Headers 字段作为HTTP_USER_AGENT
参数传递。除了 HTTP 请求 Headers 字段外,还可以使用fastcgi_param指令传递任意参数。
Embedded Variables
ngx_http_fastcgi_module
模块支持嵌入式变量,可用于使用fastcgi_param指令设置参数:
$fastcgi_script_name
- 请求 URI,或者如果 URI 以斜杠结尾,则请求 URI 带有由附加在其上的fastcgi_index伪指令配置的索引文件名。此变量可用于设置确定 PHP 中脚本名称的
SCRIPT_FILENAME
和PATH_TRANSLATED
参数。例如,对于带有以下指令的“/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
”。
使用fastcgi_split_path_info指令时,$fastcgi_script_name
变量等于该指令设置的第一个捕获的值。
$fastcgi_path_info
- fastcgi_split_path_info指令设置的第二个捕获的值。此变量可用于设置
PATH_INFO
参数。