Caching with Nginx

 

1.  General Information

 

1.1  What is a Cache?

A cache is a temporary storage that makes retrieving data from a storage more efficient. This allows various requests to be answered directly without the need for calculations. The result of the first calculation of the request is already in the cache and can thus be delivered faster. It is important to note that there are different types of caches. In web applications, these primarily include server cache (in this case, the Nginx cache), application cache, and browser cache.

Server-side, dynamic content especially benefits from caching because it usually has to be generated with a lot of effort and is often associated with database queries in the background.

 

1.2  Nginx Cache at Emubox

At Emubox, you can use Nginx as a server-side HTTP cache in Shared Hosting (Smart and Business Web Hosting) as well as in all Cloud Server offers. In Shared Hosting, Nginx can be activated in addition to the Apache web server to cache dynamic content. On Cloud Servers, Nginx can also be operated as a web server and cache in front of your own application.

Caching can be activated and configured for all products in the Emubox Control Panel in the website settings. Whether and how long content may be cached is usually determined via HTTP headers.

 

1.3  HTTP Header

Setting the corresponding HTTP headers is crucial when it comes to caching. They instruct all possible caches whether and how long the content may remain in the cache. The headers not only influence the behavior of a server-side cache like Nginx, but they also control the behavior of intermediary proxies and ultimately the browser cache. HTTP headers that influence caching include e.g. Cache-Control and Expire.

Requests without the appropriate headers are usually not cached. This is particularly the case at Emubox with Nginx, unless explicitly configured otherwise in the Emubox Control Panel.

The Nginx cache at Emubox by default only stores dynamic content because static content can usually be delivered directly from the fast filesystem cache. Nevertheless, it can be worthwhile to set appropriate HTTP headers for static content such as images. This allows the browser to keep these contents longer in its local cache.

 

1.4  Meaningful Cache Contents and Times

Before configuring the cache and adjusting your web application, you should think about what you want to store in the cache and for how long. The management of the cache usually takes place via HTTP headers (see above).

It is advisable to keep the cache time low for content that changes often. For a news page, this is important, for example, because otherwise, the changes would only be visible after the expiry of the cache time. In contrast, websites that are rarely updated can be kept in the cache for a significantly longer time.

It should also be noted that the first call after the cache time expires may take a bit longer because the page needs to be recalculated. Therefore, the following guideline is recommended: set the cache as long as possible, but as short as necessary.

Please note that while there is the option in the Emubox Control Panel to clear the Nginx cache, this has no effect on any proxies and the browser cache. Actively clearing the Nginx cache helps only to a limited extent if content is allowed to be cached for too long.

 

1.5  Password-Protected Areas

Basically, areas with password protection are not cached by Nginx for security reasons.

Warning:  If a page is already present in the cache and the password protection is set afterwards, then the cached content remains accessible without a password until its expiration. Therefore, after creating a password protection, it is advisable to clear the Nginx cache via the Emubox Control Panel.

1.6  Clearing the Cache

In some cases, it may be advisable to clear the Nginx cache in the Emubox Control Panel. For example, if adjustments have been made to the website, but they are not yet visible, and the browser cache and application cache have already been cleared. Some Nginx plugins for CMS like WordPress offer the option to also delete the Nginx cache. However, these are not supported by Emubox. Therefore, clearing must take place in any case via the Emubox Control Panel.

 


 

2.  Activation and Configuration

In the following sections, we will explain what steps you need to take to activate caching for your website.

 

2.1  Basic Settings of the Website

A prerequisite for caching is that your website is configured with Nginx as the primary web server. You can specify this directly when creating a new website in the Emubox Control Panel. For an existing website, you need to edit it and click on "Switch to Basic Settings» to activate Nginx.

  1. To do this, go to the category «Websites & Domains» > «Dashboard» > «PHP».



  2. In both cases, you must select a use case that supports caching:
    • FPM Application (Apache)
      > Optimal for static and dynamic websites (recommended)
    • FPM Application (Nginx)
      > Optimal for static websites
    • FastCGI Application (Apache)
      > For static and dynamic websites



In general, «FPM Application (Apache)» is considered the better choice when it comes to PHP applications, as it is specifically designed for this purpose.

The use case «FPM Application (Nginx)» uses Nginx; however, since it only supports static content already covered by the fast filesystem cache, additional caching there would not provide any advantages and is therefore not recommended.

«FastCGI Application (Apache)» is a further development of the outdated CGI and is somewhat less performant compared to FPM.

 

2.2  Enable and Configure Caching

Once the basic configuration of your website has been adjusted to generally support caching, you also need to explicitly activate it in the Emubox Control Panel in the website settings. You will find the corresponding options in the «Hosting and DNS» section and then «Apache and nginx».



Scroll down to the Nginx settings and enable the cache.



Please note that after enabling the cache, requests will only be stored in it if your application (e.g. your CMS) allows this via the appropriate HTTP headers. By default, Nginx does not store requests that do not have such headers. You can change this behavior with the «Fallback Behavior» setting, and you can specify that content without the appropriate headers should also be cached. However, this should only be done in exceptional cases. Normally, the route via HTTP headers is preferred, as it allows much more precise control over what should remain in the cache for how long in web applications. This setting does not affect content with HTTP headers.

We usually recommend allowing only dynamic content for Nginx caching, as mostly only dynamic content benefits from caching. Static content is already cached very efficiently by the filesystem and would only unnecessarily fill the Nginx cache.

After you have configured your cache settings, activate them by clicking «OK ».

 


 

3.  Customize Web Application

To ensure that caching works, your application must set HTTP headers that inform Nginx whether and how long the corresponding content may be stored in the cache. If no such headers are set, Nginx by default does not store anything in the cache. Although you can adjust this behavior by configuring the fallback behavior of the cache, this is usually not recommended. You should ensure that your web application sets the appropriate headers for cacheable content.

For most common CMS, there are plugins that can take care of this for you. Below are some examples:

 

WordPress:

 

Joomla!:

For self-written applications, adjustments to the code may be necessary to set the headers correctly. In some cases, the headers can also be set via « .htaccess ».


Was this article helpful?

mood_bad Dislike 0
mood Like 0
visibility Views: 456



Unable to find what you were looking for?

Our support experts are happy to assist you personally!



CONTACT SUPPORT