Using Varnish+esi to implement local caching of static pages

Source: Internet
Author: User
Tags memcached varnish haproxy
Page static is to build High-performance Web site must use one of the moves, the page static can effectively improve the system response speed, but also conducive to search engine optimization. However, after the page is static, static pages contain (for example, all static pages including headers, footers), and dynamic updates of local information in static pages become new problems.

The inclusion between static pages generally has the following scenarios:

1,Client Side Includes (CSI): through frame, iframe, JavaScript, Javacript+ajax and other ways of the content of another page dynamically included. JavaScript libraries, such as the popular jquery, now have better support.

Advantages: It can utilize the mechanism of parallel processing and loading of browser client, and can reduce network transmission time and improve performance by browser caching mechanism, and can reduce server-side pressure by computing on client.

Disadvantages: Search engine optimization problems; JavaScript compatibility issues; Client caching may cause server-side content to be updated and not effective in time;

2.Server Side Includes (SSI):

Advantages: SSI Technology is a universal technology, not subject to specific language constraints, only the Web server or application server support, Ngnix, Apache, Tomcat, JBoss and so on have better support

Disadvantage: SSI is not syntactically capable of directly containing the URLs of other servers (and, of course, it can be implemented by redirect, etc.), so it is relatively inflexible in environments where caching and load balancing are needed to be fully leveraged.

Of course, if you do not use a separate caching server, instead of using Ngnix, you can use Ngnix for SSI and memcached support, and page caching through Nginxhttpssimodule, Nginxhttpmemcachedmodule, However, compared with professional caching servers (such as varnish), Ngnix is only suitable for small and medium sized occasions as a caching server.

3, Edge Side Includes (ESI):

Edge Side Includes (ESI) and server Side Includes (SSI) and functionality are similar. SSI requires a special file suffix (shtml,inc). ESI can contain remote server files directly through URIs, and ESI is better suited for caching servers, caching entire pages or page fragments, so ESI is especially good for caching. Varnish, like the current cache server, supports this.

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

Most Web sites have the requirement to have content clips that need to be updated dynamically throughout the static page, including:

1, and user-independent information, all users come in to see the same content. such as the hottest news, the most active users, etc.

2, and personalized information related to users. such as user login information, user friends, etc.

The above two cases, generally using AJAX methods to achieve static page local information refresh, Ajax directly to the Web server or application server to obtain dynamic data. Or use ajax+memcached mode, the dynamic content into the memcached, Ajax direct access to memcached, which can alleviate the Web server or application server pressure. However, the Ajax solution bypasses the caching server and does not fully utilize the caching support of the cached server for static pages.

The use of varnish and its support for ESI is easy to achieve better support for both of these requirements:

1, and user-independent information: directly by the Varnish+esi can be achieved. For dynamically changing local pages, the caching policy can be specified in the Cache-control of the HTTP header returned by the URL address that is developed by ESI to implement local page caching (fragment caching).

2, and personalized information related to the user: for the entire page of the caching strategy is still used Varnish+esi way. Instead of a local page that requires fragment caching, you can obtain user identity information (such as UserID) based on user cookie information, and then bring user identity information to the back-end Web server or application server in the URL of ESI to obtain information that is personalized to the user. Refer to: Caching logged in users.

A simple comb the idea of implementing static page caching based on Varnish+esi, and then write code to test the specific.

Varnish as a high-performance caching server, it is worth a good study.

Although both varnish and Ngnix have load balancing capabilities, Ngnix can only load balance based on client IP, does not support session state maintenance (session persistence), and cannot maintain session state , and the varnish load balancing is not supported. And that's what Haproxy's strength is.

This leads to a relatively desirable architecture:

Nginx (for HTTP compression and HTTPS) –> varnish (for reverse proxy caching) –>haproxy (used as load Balancing) –>ngnix (Web Server) or Tomcat

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.