ngx_http_fastcgi_module

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;
}

Directives

Variables

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

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_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.

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;

fastcgi_pass_header

syntax: fastcgi_pass_header name

context: http, server, location

fastcgi_read_timeout

syntax: fastcgi_read_timeout time

default: 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 from fastcgi_intercept_errors.

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.

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_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 or 8K.

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 buffersize is equal to the size of one buffer in directive fastcgi_buffers; however, it is possible to set it to less.

fastcgi_temp_path

syntax: fastcgi_temp_path dir-path [ level1 [ level2 [ level3 ] ] ];

default: $NGX_PREFIX/fastcgi_temp controlled by --http-fastcgi-temp-path at ./configure stage

context: http, server, location

This directive works like client_body_temp_path to specify a location to buffer large proxied requests to the filesystem.

Variables

In the ngx_http_fastcgi_module there is one built-in variable, which can be used for the formation of the parameters with the aid of directive fastcgi_param:

$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".

References

Original Documentation

NginxHttpFcgiModule (last edited 2008-03-21 01:33:15 by Schultz)