The Fastcgi_cache of Nginx cache technology

Source: Internet
Author: User
Tags response code nginx server

A few words of nagging:

Nginx Cache features are: Proxy_cache/fastcgi_cache

The role of Proxy_cache is to cache the contents of the backend server, which may be anything, including static and dynamic.
The role of Fastcgi_cache is to cache fastcgi generated content, and in many cases it is the dynamic content generated by PHP.
The Proxy_cache cache reduces the number of nginx-to-backend communications, saving transmission time and back-end bandwidth.
The Fastcgi_cache cache reduces the number of nginx-to-PHP communications and eases the pressure on PHP and the database (MySQL), which is much easier than caching with memcached.
However, the cache also has drawbacks, such as comments can not be displayed at that time, and so on, their own choice, there will be lost.


FastCGI Module (FastCGI)



This module allows Nginx to work in conjunction with fastcgi and control which parameters are passed safely.
Cases:
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;}
An instance in the cache:
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;    }  }}
After 0.7.48, the cache follows the backend server's Cache-control, expires, etc. after 0.7.66, the "Cache-control:" Private "and" No-store "headers are also followed. • Instruction Fastcgi_ Buffer_size syntax: Fastcgi_buffer_size the_size;
Default value: Fastcgi_buffer_size 4k/8k;
Using fields: HTTP, server, location
This parameter specifies how much buffer will be used to read the first part of the incoming reply from the fastcgi server.
In general, a small answer header is included in this section.
The default buffer size is the size of each block in the Fastcgi_buffers directive, which can be set to a smaller value. Fastcgi_buffers syntax: fastcgi_buffers the_number is_size;
Default value: Fastcgi_buffers 8 4k/8k;
Using fields: HTTP, server, location
This parameter specifies the response from the FASTCGI server, how many buffers will be read locally, and by default this parameter is equal to the paging size, which may be 4K, 8 K or 16K depending on the environment. Fastcgi_cache syntax: Fastcgi_cache zone|off;
Default value: Off
Using fields: HTTP, server, location
Specify a region for the shared memory that is actually used by the cache, and the same area can be used in different places. Fastcgi_cache_key Syntax: Fastcgi_cache_key line
Default value: None
Using fields: HTTP, server, location
Set the cached keyword, such as:
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 value: None
Using fields: HTTP
The Clean_time parameter has been removed in version 0.7.45.
This directive specifies the path of the fastcgi cache and other parameters, all of which are stored as files, the cached key (key), and the MD5 value computed by the file name as the proxy URL.
The level parameter sets the directory rating of the cache directory and the number of subdirectories, such as directives if set to:
Fastcgi_cache_path/data/nginx/cache Levels=1:2 keys_zone=one:10m;
Then the data file will be stored as:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
The files in the cache are first written to a temporary file and then moved to the last position of the cache directory, 0.8. After version 9, you can store temporary files and cache files on different file systems, but you need to understand that this movement is not a simple atomic renaming system call, but a copy of the entire file. Therefore, it is best to use the same file system in the values of Fastcgi_temp_path and Fastcgi_cache_path.
In addition, all active keywords and data related information are stored in the shared memory pool, the name and size of this value is specified by the Key_zone parameter, the inactive parameter specifies the memory time of the data stored, default is 10 minutes.
The max_size parameter sets the maximum value of the cache, and a specified cache manager process periodically deletes the old cached data. Fastcgi_cache_methods syntax: fastcgi_cache_methods [GET HEAD POST];
Default value: Fastcgi_cache_methods GET HEAD;
Use field: Main,http,location
Get/head cannot be disabled, even if you just set it up:
Fastcgi_cache_methods POST;
fastcgi_cache_min_uses Syntax: fastcgi_cache_min_uses n
Default value: Fastcgi_cache_min_uses 1
Using fields: HTTP, server, location
The instruction specifies how many times the same URL that is requested will be cached. Fastcgi_cache_path syntax: fastcgi_cache_path/path/to/cache [levels=m:n keys_zone=name:time Inactive=time]
Default value: None
Using fields: HTTP
The Clean_time parameter was removed in version 0.7.45
Unknown. Fastcgi_cache_use_stale syntax: Fastcgi_cache_use_stale [updating|error|timeout|invalid_header|http_500]
Default value: Fastcgi_cache_use_stale off;
Using fields: HTTP, server, location
Unknown. Fastcgi_cache_valid syntax: fastcgi_cache_valid [Http_error_code|time]
Default value: None
Using fields: HTTP, server, location
Specifies the cache time for the specified HTTP return code, for example:
Fastcgi_cache_valid 302 10m;fastcgi_cache_valid 404 1m;
The response status code of 200 and 302 is cached for 10 minutes, and 404 is cached for 1 minutes.
By default, the cache handles only 200,301,302 of the state.
You can also use any to denote any one of the commands.
Fastcgi_cache_valid 302 10m;fastcgi_cache_valid 301 1h;fastcgi_cache_valid any 1m;
Fastcgi_connect_timeout Syntax: fastcgi_connect_timeout time
Default value: Fastcgi_connect_timeout 60
Using fields: HTTP, server, location
Specifies the connection time-out period for the FASTCGI server, which cannot exceed 75 seconds. Fastcgi_index Syntax: Fastcgi_index file
Default value: None
Using fields: HTTP, server, location
If the URI ends with a slash, the file name is appended to the URI, and the value is stored in the variable $fastcgi_script_name. For example:
Fastcgi_index Index.php;fastcgi_param Script_filename/home/www/scripts/php$fastcgi_script_name;
The parameter script_filename for request "/page.php" will be set to "/home/www/scripts/php/page.php", but "/" is "/home/www/scripts/php/index.php". Fastcgi_hide_header Syntax: Fastcgi_hide_header name
Using fields: HTTP, server, location
By default, Nginx does not transfer the "Status" and "X-accel-..." headers from the FASTCGI server to the client, which can also hide some other headers.
If you must pass the "Status" and "X-accel-..." headers, you must use Fastcgi_pass_header to force them to be delivered to the client.
Fastcgi_ignore_client_abort Syntax: Fastcgi_ignore_client_abort on|off
Default value: Fastcgi_ignore_client_abort off
Using fields: HTTP, server, location
If the current connection request FASTCGI server fails, you can use this command to prevent it from disconnecting from the Nginx server. Fastcgi_ignore_headers Syntax: fastcgi_ignore_headers name [name ...]
Using fields: HTTP, server, location
This directive prohibits processing some fastcgi server-answered header fields, such as "X-accel-redirect", "X-accel-expires", "Expires" or "Cache-control". Fastcgi_intercept_errors Syntax: fastcgi_intercept_errors on|off
Default value: Fastcgi_intercept_errors off
Using fields: HTTP, server, location
This directive specifies whether to pass 4xx and 5xx error messages to the client, or to allow Nginx to handle error messages using Error_page.
You must specify the processing method in the Error_page to make this parameter valid, as Igor said, "If there is no proper handling method, Nginx will not intercept an error, this error will not display its own default page, which allows some methods to intercept the error."
Fastcgi_max_temp_file_size Syntax: fastcgi_max_temp_file_size 0
Default value:?
Use fields:?
Turn off fastcgi buffering based on source code.
Fastcgi_no_cache syntax: Fastcgi_no_cache variable [...]
Default value: None
Using fields: HTTP, server, location
Determine under what circumstances the cached answer will not be used, example:
Fastcgi_no_cache $cookie _nocache $arg _nocache$arg_comment; Fastcgi_no_cache $http _pragma $http _authorization;
If an empty string or equal to 0, the value of the expression equals false, for example, in the above example, if the cookie "NoCache" is set in the request, the cache is bypassed. Fastcgi_next_upstream Syntax: Fastcgi_next_upstream error|timeout|invalid_header|http_500|http_503|http_404|off
Default value: Fastcgi_next_upstream Error timeout
Using fields: HTTP, server, location
directive specifies which case requests will be forwarded to the next FASTCGI server:
The request in the error-transfer or the request that is reading the answer header has an error while connecting to the server.
The request in the timeout-transfer or the request that is reading the answer header times out when connecting to the server.
The invalid_header-server returns an empty or invalid reply.
The http_500-server returns a 500 response code.
The http_503-server returns a 503 response code.
The http_404-server returns a 404 response code.
off-prohibits requests from being routed to the next fastcgi server.
Note that the transfer request may have sent the empty data to the client before it is routed to the next server, so if there is an error in the data transfer or the timeout occurs, the instruction may not be able to fix some of the delivery errors. Fastcgi_param syntax: Fastcgi_param parameter value
Default value: None
Using fields: HTTP, server, location
Specifies some parameters that are passed to the FASTCGI server.
You can use strings, variables, or combinations, where the settings do not inherit from the other fields, and the current field will erase any previous definition.
The following is a minimum parameter that PHP needs to use:
Fastcgi_param Script_filename/home/www/scripts/php$fastcgi_script_name; Fastcgi_param query_string $query _string;
PHP uses the Script_filename parameter to determine which script to execute, Query_string contains some of the parameters in the request.
If you are processing a POST request, you need to add three additional parameters:
Fastcgi_param Request_method $request _method;  Fastcgi_param Content_Type $content _type; Fastcgi_param content_length $content _length;
If PHP has--enable-force-cgi-redirect at compile time, you must pass the Redirect_status parameter with a value of 200:
Fastcgi_param Redirect_status 200;
Fastcgi_pass Syntax: Fastcgi_pass fastcgi-server
Default value: None
Using fields: HTTP, server, location
Specifies that the FASTCGI server listens on ports and addresses, which can be native or otherwise:
Fastcgi_pass localhost:9000;
Using a UNIX socket:
Fastcgi_pass Unix:/tmp/fastcgi.socket;
You can also use a upstream field name:
Upstream backend {server localhost:1234;} fastcgi_pass backend;
Fastcgi_pass_header Syntax: Fastcgi_pass_header name
Default value: None
Using fields: HTTP, server, location
Fastcgi_read_timeout Syntax: fastcgi_read_timeout time
Default value: Fastcgi_read_timeout 60
Using fields: HTTP, server, location
The response time-out time of the front-end fastcgi server, which may need to be adjusted if there are long-running fastcgi processes that are not output until they run out, or if a front-end server response time-out error occurs in the error log. Fastcgi_redirect_errors Syntax: fastcgi_redirect_errors on|off
The instruction is renamed to Fastcgi_intercept_errors. Fastcgi_send_timeout Syntax: fastcgi_send_timeout time
Default value: Fastcgi_send_timeout 60
Using fields: HTTP, server, location
The instruction sets the upstream server to wait for a fastcgi process to transfer data time, if there are some long-running fastcgi processes that are not output until they run out, then you can modify this value if you find some timeout errors in the error log on the server. Then you can increase the value appropriately.
The instruction specifies the time-out of the request server, which is the connection that completes the 2 handshake, not the full connection, and if the client does not pass data during this period, the server shuts down the connection. Fastcgi_split_path_info Syntax: Fastcgi_split_path_info regex
Working with Fields: location
Available versions: 0.7.31 or more, 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, ...}
The parameter script_filename for request "/show.php/article/0001" will be set to "/path/to/php/show.php" and the parameter Path_info to "/article/0001". Fastcgi_store syntax: Fastcgi_store [on | off | path]
Default value: Fastcgi_store off
Using fields: HTTP, server, location
The path to the storage front-end file is specified, the parameter on specifies the same path as the root and alias directives, the off is prohibited, and in addition, variables can be used to make the path name more explicit:
Fastcgi_store/data/www$original_uri;
The "last-modified" header in the answer sets the last modification time of the file, in order to make these files more secure, you can save them in a directory as temporary files, using the Fastcgi_temp_path directive.
This instruction can be used in the process of creating a local copy of a back-end dynamic output that is not often changed. Such as:
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 is not a cache, it is more like a mirror under certain requirements. Fastcgi_store_access syntax: fastcgi_store_access users:permissions [users:permission ...]
Default value: Fastcgi_store_access USER:RW
Using fields: HTTP, server, location
This parameter specifies the permissions to create a file or directory, for example:
Fastcgi_store_access USER:RW GROUP:RW all:r;
If you want to specify permissions for a group of people, you can not write users, such as:
Fastcgi_store_access GROUP:RW all:r;
Fastcgi_temp_path Syntax: Fastcgi_temp_path path [Level1 [Level2 [LEVEL3]]
Default value: Fastcgi_temp_path fastcgi_temp
Using fields: HTTP, server, location
Directives specify the path of temporary files that store data that is sent from other servers, you can also specify that the level three directory is already hashed, and that the value of levels specifies how many tokens are set for the hash, for example, in the following configurations:
Fastcgi_temp_path/spool/nginx/fastcgi_temp 1 2;
The temporary files are similar to the following:
/spool/nginx/fastcgi_temp/7/45/00000123457
• The relevant parameter request headers that are transmitted to the FASTCGI server are routed to the FASTCGI server in the form of parameters, which are run on the FASTCGI server with specific applications and scripts, which are usually obtained as environment variables, for example, "User-agent" header Http_user_ The agent parameter is passed, and there are some other HTTP headers that can be passed freely with the fastcgi_param instruction. • Variable $fastcgi_script_name this variable equals a request URI that ends with a slash, plus fastcgi_ Index the given argument. You can use this variable instead of Script_filename and path_translated to determine the name of the PHP script.
In the following example, request "/info/":
Fastcgi_index index.php; Fastcgi_param Script_filename/home/www/scripts/php$fastcgi_script_name;
Script_filename equals "/home/www/scripts/php/info/index.php".


This article is from the "Common Documents" blog, so be sure to keep this source http://yujianglei.blog.51cto.com/7215578/1565576

The Fastcgi_cache of Nginx cache technology

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.