Using Varnish + ESI to implement local caching of static pages (IDEA)

Source: Internet
Author: User
Tags varnish haproxy

 

Static pages are one of the essential features for building high-performance websites. Static pages can effectively improve the system response speed and facilitate search engine optimization. However, after static pages are made, dynamic updates of static pages (for example, all static pages contain the header and footer) and local information in static pages become a new problem.

Static pages include the following solutions:

1,Client Side Includes (CSI ):Dynamically include the content of another page through frame, iframe, javascript, javacript + ajax, etc. Javascript libraries such as jquery are now well supported.

Advantages: the browser client can be used for Parallel Processing and loading; the browser cache mechanism can reduce network transmission time and improve performance; the computing is placed on the client, which can reduce the pressure on the server.

Disadvantages: search engine optimization, javascript compatibility, client cache, and other security risks

2,Server Side encryption DES (SSI ):

Advantages: SSI technology is a general technology and is not restricted by specific languages. It only requires support from Web servers or application servers. Ngnix, Apache, Tomcat, and Jboss all provide better support for this technology.

Disadvantage: SSI syntax cannot directly contain URLs of other servers (of course, it can also be implemented through redirect and other alternatives ), therefore, it is not flexible to make full use of the cache and Server Load balancer.

Of course, if you do not use a separate cache server, but use Ngnix to support SSI and Memcached, you can also implement page caching through NginxHttpSsiModule and NginxHttpMemcachedModule, however, compared with professional cache servers (such as Varnish), Ngnix is only suitable for small and medium sized applications.

3. Edge Side primary des (ESI ):

Edge Side primary des (ESI) And Server Side pair des (SSI) Is similar to the function. SSI requires a special file suffix (shtml, inc ). ESI can directly include remote server files through Uris. ESI is more suitable for caching the entire page or page segment on the cache server. Therefore, ESI is particularly suitable for caching. Such as the popular Cache Server Varnish.

 

SSI can easily meet the need to include other static pages for all static pages.

Most websites have the following requirements: dynamic updates are required for some parts of the static page, including:

1. Information unrelated to User Personalization. The content displayed by all users is the same. For example, the most popular news and the most active users

2. Related to user personalized information. Such as user logon information and user friends

In both cases, ajax is usually used to refresh local information on static pages. ajax is directly submitted to the Web server or application server for dynamic data. You can also use the Ajax + Memcached mode to add dynamically changed content to Memcached. ajax directly accesses Memcached, which can relieve the pressure on Web servers or application servers. However, the ajax solution directly bypasses the cache server and does not fully utilize the cache server's support for static pages.

Using Varnish and its support for ESI can easily achieve better support for the above two requirements:

1. Information unrelated to User Personalization: directly implemented by Varnish + ESI. For dynamically changed local pages, you can return the http headerCache-Control is used to specify the Cache policy and implement partial page Cache (fragment caching ).

2. Related to User Personalization: the Varnish + ESI method is still used for the cache policy for the entire page. For a local page that requires fragment caching, you can obtain user identity information (such as userid) based on user Cookie information ), then, the user identity information is added to the esi url and submitted to the backend Web server or application server for information related to User Personalization. Refer:Caching logged in users.

This article briefly combs the idea of implementing static page caching Based on Varnish + ESI, and then writes the code to test it.

As a high-performance cache server, Varnish is worth studying.

Although both Varnish and Ngnix have the Load Balancing function, Ngnix can only perform Load Balancing Based on the Client IP address, and does not support Session persistence; varnish does not support Load Balancing. This is the strength of HAproxy.

Therefore, a relatively ideal architecture is obtained:

Nginx (for HTTP compression and https)-> Varnish (for reverse proxy caching)-> HAProxy (for Load Balancing)-> Ngnix (Web Server) or Tomcat

Reference:

Http://www.trygve-lie.com/blog/entry/esi_explained_simple

Http://jimmyg.org/blog/2009/ssi-memcached-nginx.html

Http://docs.heroku.com/http-caching

Http://docs.heroku.com/memcached

Http://kovyrin.net/2007/08/05/using-nginx-ssi-and-memcache-to-make-your-web-applications-faster/

Http://www.ibm.com/developerworks/opensource/library/os-php-varnish/index.html

 

From: http://hi.baidu.com/chuanliang2007/blog/item/075f67963e20f315d31b7035.html

Related Article

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.