Recently want to use Nginx Proxy_cache cache some pages of the site, a nginx to do proxy, two Web,web station is a pseudo-static PHP page, test found Nginx can not cache the backend for pseudo-static HTML, static HTML can! Is it me where the configuration is wrong, or nginx itself can not cache pseudo-static pages?
Reply content:
Recently want to use Nginx Proxy_cache cache some pages of the site, a nginx to do proxy, two Web,web station is a pseudo-static PHP page, test found Nginx can not cache the backend for pseudo-static HTML, static HTML can! Is it me where the configuration is wrong, or nginx itself can not cache pseudo-static pages?
If the backend has Set-cookie,nginx is not cached, plus this configuration proxy_ignore_headers Set-cookie can be cached. In addition, the values of these headers (Expires, E-tags, last-modified, Cache-control) are also affected by the cache.
If the returned page does not return Expires, E-tags, Last-modified, Cache-control, and so on, this file can be cached HTTP headers, Nginx is not cached
This is the same as the browser behavior, if the server returns the HTTP header indicates that the file can be cached, nginx or browser will be cached, otherwise it is considered a dynamic page.
This time nginx-cache-purge This module is very useful, he can help you to manage and clearfastcgi_cache
Installation method
mkdir /home/cache/wpcache -pcd ~./lnmp stopapt-get install git -y #centos用yum install git -ygit clone https://github.com/FRiCKLE/ngx_cache_purgewget http://soft.vpser.net/web/nginx/nginx-1.0.15.tar.gztar zxvf nginx-1.0.15.tar.gzcd nginx-1.0.15/./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=../ngx_cache_purgemakemake install/root/lnmp start
Here I take WordPress as an example, send a configuration method
http
#在http层添加以下三行代码fastcgi_cache_path /home/cache/wpcache levels=1:2 keys_zone=wpcache:10m inactive=20m;fastcgi_cache_key "$scheme$request_method$host$request_uri";fastcgi_cache_use_stale error timeout invalid_header http_500;#wpcache为名字,可以随便修改。10m为内存占用
Server
Set $no _cache 0; # do not cache POST operation if ($request _method = post) {set $no _cache 1; } if ($query _string! = "") {set $no _cache 1; Do not cache the background if ($request _uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail). php|wp-.*.php|/feed/|in Dex.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap (_index)?. xml| [A-z0-9_-]+-sitemap ([0-9]+)?. XML) {set $no _cache 1; # login does not cache (prevent message string) if ($http _cookie ~* "COMMENT_AUTHOR|WORDPRESS_[A-F0-9]+|WP-POSTPASS|WORDPRESS_NO_CACHE|WORDPR Ess_logged_in ") {set $no _cache 1; } location/{try_files $uri $uri//index.php? $args; } location ~. php$ {try_files $uri/index.php; Include Fastcgi_params; #Lnmp. org One-click Package to include fcgi.conf; Fastcgi_pass Unix:/var/run/php5-fpm.sock; #Lnmp. org one-key package changed to Fastcgi_pass Unix:/tmp/php-cgi.sock; Fastcgi_cache_bypass $no _cache; Fastcgi_no_cache $no _cache; Fastcgi_cacHe wpcache; #要跟前面设置的名称一样 Fastcgi_cache_valid 30m; #缓存时间} location ~/purge (/.*) {allow 11.22.33.44; #此处该为你vps的ip allow 8.8.8.8; Allow 127.0.0.1; Deny all; Fastcgi_cache_purge Wpcache "$scheme $request_method$host$1"; }
It is estimated that you understand the cache in PHP-FPM.
Nginx not only has a very familiar with the cache agent backend content of the Proxy_cache, there is a lot of people ignore the 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.
Via http://www.linuxyan.com/web-server/78.html
http://segmentfault.com/blog/qianfeng/1190000000430660
Originally wanted to talk about Fastcgi_cache, but @ Li Wei a lengthy, robbed my jackpot.
If those pseudo-static pages are not often changed, in the HTTP header is also configured on the cache, even Nginx has saved a lot of things.