Caching with NGINX® with Reverse Proxy

Last modified: 2025 February 10


Overview

This document covers information about caching in NGINX® with reverse proxy.

Caching

NGINX reverse proxies to Apache and caches all requests by default.

NGINX caches data on a per-user basis. The cache file is stored in the following location, where type represents the type of caching, and username represents the username:

/var/cache/ea-nginx/proxy/username

NGINX with reverse proxy uses the type proxy for the proxy_pass directive. The cache directories use 700 permissions, restricted to the nobody and root users.

Your application’s requests for dynamic content should include cache control headers. If they do not, the server may return cached responses that do not apply to the user.

To correct this issue, perform one of the following actions:

  • Disable caching for the user in WHM’s NGINX Manager interface (WHM » Home » Software » NGINX Manager).
  • Modify your web application to use the appropriate headers so NGINX does not cache the request.

Custom cache keys

You can set your NGINX cache to generate separate caches for different conditions. For example, you may want to cache mobile and non-mobile requests separately.

To configure your cache to use separate caches in varying conditions, set the $CACHE_KEY_PREFIX in the /etc/nginx/conf.d/includes-optional/set-CACHE_KEY_PREFIX.conf file. We include this file by default, with the option to cache mobile requests separately commented out.

For more information about proxies in NGINX, read the NGINX Proxy documentation.

Use htaccess to manage caching

You can also use your .htaccess files to manage caching in NGINX. If you use this method, your .htaccess file will set the header in your requests to handle caching for any requests your application does not set.

Your applications should set appropriate headers to ensure that sensitive data is not cached. If you cannot update your application to send appropriate headers, then you can use your .htaccess file. The entry in your .htaccess file might resemble the following example:

1
2
3
4
5
6
7
8
# Broken-app does not set proper headers and sends private data. This needs to be updated in the application and this section removed.
<If "%{REQUEST_URI} =~ m#/payment-methods/#">
    <IfModule mod_headers.c>
        Header set Pragma "no-cache"
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
        Header set Expires "Thu, 1 Jan 1970 00:00:00 GMT"
    </IfModule>
</If>
Important:

If your application does not use proper headers for sensitive data, it is a security risk.

Add the following to your .htaccess file to enable caching for common static files:

1
2
3
<filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=3600, public"
</filesMatch>
Remember:

Your application’s requests for dynamic content should include cache control headers. If they do not, the server may return cached responses that do not apply to the user.

Third-party integration

Third-party integrators can use the following information to determine if NGINX caching is active. You can only use caching if you install the ea-nginx package. We also display the caching status in the NGINX Caching section of the cPanel Interface.

If the /etc/nginx/ea-nginx/cache.json file exists, you can determine if caching is enabled or disabled based on the following information:

  • If the enabled key exists in the following file, its value determines if NGINX caching is enabled. In this example, user represents the username:
    /var/cpanel/userdata/user/nginx-cache.json
  • If the enabled key exists in the following global configuration file, its value determines if NGINX caching is enabled. In this example, user represents the username:
    /etc/nginx/ea-nginx/cache.json
  • If the enabled key does not exist in either file, then caching is enabled by default.

If the /etc/nginx/ea-nginx/cache.json file does not exist, then ea-nginx is not installed on the server.

If the /etc/nginx/ea-nginx/enable.standalone file exists, the ea-nginx-standalone package is installed, and caching is not enabled on the server.