Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.

Caching static content with a reverse proxy

Caching static content with a reverse proxy

To improve overall site performance and end user experience, it is recommended that caching be enabled on the storefront for static content such as images. These instructions explain how to enable caching by using Apache web server's reverse proxy feature. It assumes you are using a Tomcat application server.

Warning:

There are some security risks associated with caching content. For more information, see the articles listed after these instructions.

Requirements

The following web server/application server combinations have been tested by Elastic Path:

Apache 2.2.x Tomcat 7.0.x

Apache 2.2.x

WebLogic (using the Apache HTTP Server Plug-in)

Other web server/application server versions may or may not work.

Configuring the Reverse Proxy

  1. In Tomcat's server.xml file, ensure that there are two AJP connectors enabled. One on port 8009 and another on port 8010.
    <Connector enableLookups="false" port="8009" protocol="AJP/1.3"/commerce-legacy/>
    <Connector enableLookups="false" port="8010" protocol="AJP/1.3" scheme="https" secure="true"/commerce-legacy/>
    
    Note:

    The scheme and secure parameters are required to prevent Spring Security from redirecting requests on port 8010 to another secure port.

  2. In Apache's conf directory (on Linux, usually this is /etc/http/conf.d), edit httpd.conf file and add the following:
    Include conf/proxy_ajp.conf
    Include conf/disk_cache.conf
    
  3. In Apache's conf directory, create a proxy_ajp.conf file and add two virtual hosts, one secure and one non-secure. Your configuration should look similar to the following:
    LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    
    <VirtualHost 10.10.90.54:80>
            ServerName 10.10.90.54
            ProxyPreserveHost On
            ProxyPass /storefront ajp://10.10.90.54:8009/storefront keepalive=On
    </VirtualHost>
    
    <VirtualHost 10.10.90.54:443>
            ServerName 10.10.90.54
            # Enable/Disable SSL for this virtual host if you want to terminate SSL here
            ProxyPreserveHost On
            ProxyPass /storefront ajp://10.10.90.54:8010/storefront keepalive=On
    </VirtualHost>
    
    This proxies non-secure and secure requests to Tomcat over AJP on ports 8009 and 8010 respectively.
  4. In Apache's conf directory, create a disk_cache.conf file and add the following:
    CacheEnable disk /storefront/
    CacheRoot /var/www/cache
    CacheDirLevels 5
    CacheDirLength 2
    CacheIgnoreHeaders Set-Cookie
    
    This creates a file cache in /var/www/cache and enables caching of requests that include cache control headers, such as max-age.
  5. Confirm that the storefront's Spring Security configuration contains the proper port mappings.
  6. Reload Apache.
    /etc/init.d/httpd reload
    

Using the Caching Control Filter

You can use the Caching Control Filter to add the max-age cache control header to requests for specific types of content (based on URL patterns). For example, by default, the filter is configured to add max-age cache control headers to requests that contain template-resources, which includes the theme's Javascript and stylesheet files.

The Caching Control Filter configuration is in the storefront's ep-storefront\src\main\resources\spring\web\filter-config.xml file, in the cachingControlFilter bean definition. The cachingControlEntries list contains bean definitions that represent the URL patterns to test and max-age value to set.

<bean id="cachingControlFilter"
	class="com.elasticpath.commons.filter.impl.CachingControlFilter">
	<property name="cachingControlEntries">
		<list>
			<bean class="com.elasticpath.commons.filter.impl.CachingControlFilter$CachingControlEntry">
				<property name="urlPattern">
					<value>^.*renderImage\.image.*$</value>
				</property>
				<property name="maxAge">
					<value>86400</value>
				</property>
			</bean>
			<bean class="com.elasticpath.commons.filter.impl.CachingControlFilter$CachingControlEntry">
				<property name="urlPattern">
					<value>^.*template-resources.*$</value>
				</property>
				<property name="maxAge">
					<value>86400</value>
				</property>
			</bean>
			<bean class="com.elasticpath.commons.filter.impl.CachingControlFilter$CachingControlEntry">
                		<property name="urlPattern">
                        		<value>^.*content.*$</value>
				</property>
				<property name="maxAge">
					<value>86400</value>
				</property>
			</bean>
		</list>
	</property>
</bean>

For more detailed information on caching and load balancing using Apache modules, see the following pages: