Can nginx not cache pseudo-static Web pages?

Source: Internet
Author: User
Recently, I want to use nginx's proxy_cache to cache some pages of a website. One nginx is used as a proxy and two web servers. The web site is a PHP pseudo static page, during the test, we found that nginx could not cache pseudo-static html at the backend, and static html could! Is it because I have misconfigured nginx... recently, I want to use nginx's proxy_cache to cache some pages of a website. One nginx is used as a proxy and two web servers. The web site is a PHP pseudo static page, during the test, we found that nginx could not cache pseudo-static html at the backend, and static html could! Where is my configuration error, or does nginx itself fail to cache pseudo static pages?

Reply content:

Recently, I want to use nginx's proxy_cache to cache some pages of a website. One nginx is used as a proxy and two web servers. The web site is a PHP pseudo static page, during the test, we found that nginx could not cache pseudo-static html at the backend, and static html could! Where is my configuration error, or does nginx itself fail to cache pseudo static pages?

If the backend has a set-cookie, nginx will not be cached. With this configuration, proxy_ignore_headers Set-Cookie can be cached. In addition, the values of these headers (Expires, E-Tags, Last-Modified, and Cache-Control) also affect the Cache.

If the returned page does not return Expires, E-Tags, Last-Modified, Cache-Control, and other http headers that can be cached for this file, nginx will not Cache the file.
This is the same as the browser's behavior. If the http header returned by the server indicates that the file can be cached, nginx or the browser will cache the file. Otherwise, it will be considered as a dynamic page.

At this time, the nginx-cache-purge module is very useful. It can help you manage and clearfastcgi_cache

Installation Method

Mkdir/home/cache/wpcache-pcd ~. /Lnmp stopapt-get install git-y # centos use 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 -- add-module = .. /ngx_cache_purgemakemake install/root/lnmp start

Here I use wordpress as an example to send a configuration method

Http

# Add the following three lines of code to the http layer: fastcgi_cache_path/home/cache/wpcache levels = keys_zone = wpcache: 10 m inactive = 20 m; fastcgi_cache_key "$ scheme $ request_method $ host $ request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; # The Name Of wpcache can be changed as needed. 10 MB for memory usage

Server

Set $ no_cache 0; # Do not cache POST operations if ($ request_method = POST) {set $ no_cache 1;} if ($ query_string! = "") {Set $ no_cache 1;} # if ($ request_uri ~ * "(/Wp-admin/|/xmlrpc. php |/wp-(app | cron | login | register | mail ). php | wp -. *. php |/feed/| index. 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;} # logged-on non-Cache (prevent message string numbers) if ($ http_cookie ~ * "Comment_author | wordpress _ [a-f0-9] + | wp-postpass | wordpress_no_cache | wordpress_logged_in") {set $ no_cache 1;} location/{try_files $ uri/index. php? $ Args;} location ~. Php $ {try_files $ uri/index. php; include fastcgi_params; # change the one-key package of Lnmp.org 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; # The Same name as previously set fastcgi_cache_valid 30 m; # cache time} location ~ /Purge (/. *) {allow 11.22.33.44; # the ip address 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 proxy_cache, which is familiar with Caching Proxy backend content, but also fastcgi_cache, which is ignored by many people.
Proxy_cache is used to cache the content of backend servers. It may be any content, including static and dynamic content.
Fastcgi_cache is used to cache the content generated by fastcgi. In many cases, it is the dynamic content generated by php.
The proxy_cache cache reduces the number of communications between nginx and the backend, saving the transmission time and backend bandwidth.
Fastcgi_cache reduces the number of communications between nginx and php, and reduces the pressure on php and database (mysql), which is much easier than using memcached.

Via http://www.linuxyan.com/web-server/78.html

Http://segmentfault.com/blog/qianfeng/1190000000430660

I would like to talk about fastcgi_cache, but @ Li Weiyan has made a long story and snatched my bid.
If the pseudo-static pages are not changed frequently, the cache is also configured in the HTTP Header, saving a lot of trouble for nginx.

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.