NginxHttpFcgiModule
Synopsis
This module allows Nginx to interact with FastCGI processes and control what parameters are passed to the process.
Example
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; }
Caching example:
http { fastcgi_cache_path /path/to/cache levels=1:2 keys_zone=NAME:10m inactive=5m; 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; } } }
The cache honors backend's Cache-Control, Expires, and etc. since version 0.7.48, Cache-Control: private and no-store only since 0.7.66, though. Vary handling is not implemented.
Directives
fastcgi_buffer_size
syntax: fastcgi_buffer_size the_size
default: fastcgi_buffer_size 4k/8k
context: http, server, location
This directive sets the buffersize, into which will be read the first part of the response, obtained from the fastcgi server.
In this part of response the small response-header is located, as a rule.
By default, the buffer size is equal to the size of one buffer in directive fastcgi_buffers, however it is possible to set it to less.
fastcgi_buffers
syntax: fastcgi_buffers the_number is_size
default: fastcgi_buffers 8 4k/8k
context: http, server, location
This directive sets the number and the size of buffers, into which will be read the answer, obtained from the fastcgi server. By default, the size of one buffer is equal to the size of page. Depending on platform this is either 4K, 8K or 16K.
fastcgi_cache
syntax: fastcgi_cache zone|off
default: off
context: http, server, location
The directive specifies the area which actually is the share memory's name for caching. The same area can be used in several places.
fastcgi_cache_key
syntax: fastcgi_cache_key line
default: none
context: http, server, location
The directive sets the key for caching, for example:
fastcgi_cache_key localhost:9000$request_uri;
fastcgi_cache_path
syntax: fastcgi_cache_path path [levels=m:n] keys_zone=name:size [inactive=time] [max_size=size]
default: none
context: http
The clean_time parameter was removed in 0.7.45.
This directive specifies path to the cache storage and other cache parameters. All data is stored in the files. The cache key and the name of cache file are calculated as MD5 sum of the proxied URL.
Level parameter sets number and width of the names of subdirectories used to store caching files. For example, with the directive like:
fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
the data will be stored in the following file:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
Caching data is first written to the temporary file which is then moved to the final location in a cache directory. Starting from 0.8.9 it is possible to store temporary and cache files on different file systems, but it should be kept in mind that in such a case instead of cheap and atomic rename syscall a full file copy is performed. So it's better to use the same file system in both parameters of fastcgi_temp_path and fastcgi_cache_path directives.
In addition, all active keys and information about data are kept in the shared memory zone, which name and size are specified by the options of the key_zone parameter. In case this data haven't been accessed for the time, specified in the option of inactive parameter, it is wiped out from the memory. By default inactive period is set to 10 minutes.
To maintain the maximum size of the cache, which is set by max_size parameter, a special process cache manager periodically deletes old data from the cache.
fastcgi_cache_methods
syntax: fastcgi_cache_methods [GET HEAD POST];
default: fastcgi_cache_methods GET HEAD;
context: main,http,location
GET/HEAD is syntax sugar, i.e. you can not disable GET/HEAD even if you set just
fastcgi_cache_methods POST;
fastcgi_cache_min_uses
syntax: fastcgi_cache_min_uses n
default: fastcgi_cache_min_uses 1
context: http, server, location
Directive specifies after how many requests to the same URL in will be cached.
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
Determines whether or not Nginx will serve stale cached data in case of gateway event such as error, timeout etc.
fastcgi_cache_valid
syntax: fastcgi_cache_valid [http_return_code [...]] time
default: none
context: http, server, location
Directive sets caching period for the specified http return codes. For example:
fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 404 1m;
sets caching period of 10 minutes for return codes 200 and 302 and 1 minute for the 404 code.
In case only caching period is specified:
fastcgi_cache_valid 5m;
the default behavior is to cache only replies with the codes 200, 301 and 302.
It's also possible to cache all the replies by specifying code "any":
fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1m;
fastcgi_connect_timeout
syntax: fastcgi_connect_timeout time
default: fastcgi_connect_timeout 60
context: http, server, location
Directive sets timeout period for connection with FastCGI-server. It should be noted that this value can't exceed 75 seconds.
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.
For example:
fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
for the request "/page.php" parameter SCRIPT_FILENAME will be set to "/home/www/scripts/php/page.php", but for the "/" — "/home/www/scripts/php/index.php".
fastcgi_hide_header
syntax: fastcgi_hide_header name
context: http, server, location
By default, Nginx does not pass headers "Status" and "X-Accel-..." from the FastCGI process back to the client. This directive can be used to hide other headers as well.
If the headers "Status" and "X-Accel-..." must be provided, then it is necessary to use directive fastcgi_pass_header to force them to be returned to the client.
fastcgi_ignore_client_abort
syntax: fastcgi_ignore_client_abort on|off
default: fastcgi_ignore_client_abort off
context: http, server, location
This directive determines if current request to the FastCGI-server must be aborted in case the client aborts the request to the server.
fastcgi_ignore_headers
syntax: fastcgi_ignore_headers name [name...]
context: http, server, location
This directive forbids processing of the named headers from the FastCGI-server reply. It is possible to specify headers like "X-Accel-Redirect", "X-Accel-Expires", "Expires" or "Cache-Control".
fastcgi_intercept_errors
syntax: fastcgi_intercept_errors on|off
default: fastcgi_intercept_errors off
context: http, server, location
This directive determines whether or not to transfer 4xx and 5xx errors back to the client or to allow Nginx to answer with directive error_page.
Note: You need to explicitly define the error_page handler for this for it to be useful. As Igor says, "nginx does not intercept an error if there is no custom handler for it it does not show its default pages. This allows to intercept some errors, while passing others as are."
fastcgi_max_temp_file_size
syntax: fastcgi_max_temp_file_size 0
default: ?
context: ?
This directive turns off fastcgi buffering according to the source code.
fastcgi_no_cache
syntax: fastcgi_no_cache variable [...]
default: none
context: http, server, location
Specifies in what cases the cached responses will not be used, e.g.
fastcgi_no_cache $cookie_nocache $arg_nocache$arg_comment; fastcgi_no_cache $http_pragma $http_authorization;
The expression is false if it is equal to the empty string or "0". For instance, in the above example, the cache will be bypassed if the cookie "nocache" is set in the request.
fastcgi_next_upstream
syntax: fastcgi_next_upstream error|timeout|invalid_header|http_500|http_503|http_404|off
default: fastcgi_next_upstream error timeout
context: http, server, location
This directive defines in which cases request will be passed to the next server:
- error — an error occurred during connection to the server, passing request to it or reading server respond header;
- timeout — a timeout occurred during connection to the server, passing request to it or reading server respond header;
- invalid_header — server returned empty or invalid answer;
- http_500 — server returned 500 respond;
- http_503 — server returned 503 respond;
- http_404 — server returned 404 respond;
- off — explicitly forbids passing request to the next server;
It should be clear that passing request to the next server is possible only if no data have been yet returned to the client. So, if the error or timeout occurred during the data transmission to the client it's too late to fix it.
fastcgi_param
syntax: fastcgi_param parameter value
default: none
context: http, server, location
Directive assigns the parameter, which will be transferred to the FastCGI-server.
It is possible to use strings, variables and their combination as values. Directives not set are inherited from the outer level. Directives set in current level clear any previously defined directives for the current level.
Below is an example of the minimally necessary parameters for PHP:
fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string;
Parameter SCRIPT_FILENAME is used by PHP for determining the name of script to execute, and QUERY_STRING contains the parameters of the request.
If dealing with POST requests, then the three additional parameters are necessary. Below is an example of the minimally necessary parameters for PHP:
fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length;
If PHP was compiled with --enable-force-cgi-redirect, then it is necessary to transfer parameter REDIRECT_STATUS with the value of "200":
fastcgi_param REDIRECT_STATUS 200;
fastcgi_pass
syntax: fastcgi_pass fastcgi-server
default: none
context: location, if in location
Directive assigns the port or socket on which the FastCGI-server is listening. Port can be indicated by itself or as an address and port, for example:
fastcgi_pass localhost:9000;
using a Unix domain socket:
fastcgi_pass unix:/tmp/fastcgi.socket;
You may also use an upstream block.
upstream backend { server localhost:1234; } fastcgi_pass backend;
fastcgi_pass_header
syntax: fastcgi_pass_header name
context: http, server, location
This directive explicitly allows to pass named headers to the client.
fastcgi_read_timeout
syntax: fastcgi_read_timeout time
default: fastcgi_read_timeout 60
context: http, server, location
Directive sets the amount of time for upstream to wait for a fastcgi process to send data. Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate.
fastcgi_redirect_errors
syntax: fastcgi_redirect_errors on|off
Directive is renamed into fastcgi_intercept_errors.
fastcgi_send_timeout
syntax: fastcgi_send_timeout time
default: fastcgi_send_timeout 60
context: http, server, location
Directive sets the amount of time for upstream to wait for a fastcgi process to send data. Change this directive if you have long running fastcgi processes that do not produce output until they have finished processing. If you are seeing an upstream timed out error in the error log, then increase this parameter to something more appropriate.
Directive specifies request timeout to the server. The timeout is calculated between two write operations, not for the whole request. If no data have been written during this period then serve closes the connection.
fastcgi_split_path_info
syntax: fastcgi_split_path_info regex
context: location
version: >= 0.7.31
For example:
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; ... }
for request "/show.php/article/0001" parameter SCRIPT_FILENAME will be set to "/path/to/php/show.php" and parameter PATH_INFO to "/article/0001".
fastcgi_store
syntax: fastcgi_store on|off|path
default: fastcgi_store off
context: http, server, location
This directive sets the path in which upstream files are stored. The parameter "on" preserves files in accordance with path specified in directives alias or root. The parameter "off" forbids storing. Furthermore, the name of the path can be clearly assigned with the aid of the line with the variables:
fastcgi_store /data/www$original_uri;
The time of modification for the file will be set to the date of "Last-Modified" header in the response. To be able to safe files in this directory it is necessary that the path is under the directory with temporary files, given by directive fastcgi_temp_path for the data location.
This directive can be used for creating the local copies for dynamic output of the backend which is not very often changed, for example:
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; }
To be clear fastcgi_store is not a cache, it's rather mirror on demand.
fastcgi_store_access
syntax: fastcgi_store_access users:permissions [users:permission ...]
default: fastcgi_store_access user:rw
context: http, server, location
This directive assigns the permissions for the created files and directories, for example:
fastcgi_store_access user:rw group:rw all:r;
If any rights for groups or all are assigned, then it is not necessary to assign rights for user:
fastcgi_store_access group:rw all:r;
fastcgi_temp_path
syntax: fastcgi_temp_path path [level1 [level2 [level3]]]
default: fastcgi_temp_path fastcgi_temp
context: http, server, location
This directive sets the path where to store temporary files received from another server. It is possible to use up to 3 levels of subdirectories to create hashed storage. Level value specifies how many symbols will be used for hashing. For example, in the following configuration:
fastcgi_temp_path /spool/nginx/fastcgi_temp 1 2;
Temporary file name may look like:
/spool/nginx/fastcgi_temp/7/45/00000123457
Parameters, transferred to FastCGI-server.
The request headers are transferred to the FastCGI-server in the form of parameters. In the applications and the scripts run from the FastCGI-server, these parameters are usually accessible in the form of environment variables. For example, the header "User-agent" is transferred as parameter HTTP_USER_AGENT. Besides the headers of the HTTP request, it is possible to transfer arbitrary parameters with the aid of directive fastcgi_param.
Variables
$fastcgi_script_name
This variable is equal to the URI request or, if if the URI concludes with a forward slash, then the URI request plus the name of the index file given by fastcgi_index. It is possible to use this variable in place of both SCRIPT_FILENAME and PATH_TRANSLATED, utilized, in particular, for determining the name of the script in PHP.
For example, for the request "/info/":
fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
SCRIPT_FILENAME would equal "/home/www/scripts/php/info/index.php".












