How to Configure Server Cache on Nginx

Nginx server cache with proxy_pass

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you will learn how to implement server-level caching on an Nginx server though a basic example. I will assume that you already have an existing Nginx web server.

By the end of this tutorial, you will have a fully functioning Nginx reverse proxy caching server that communicates with your website via an HTTP socket.

1. Change the Port Your Website Listens On

Because the caching server will handle all incoming requests, you want to have this server listening on port 80 for HTTP websites or port 443 for HTTPS websites. This means that your website will need to listen on another port.

Change the default listen port to 8000 in the appropriate Nginx configuration file for your website. This is usually a .conf file in the /etc/nginx/sites-available/ directory.

2. Create an Nginx Reverse Proxy Server

Next, let’s set up the Nginx cache server. At its core, this will be a reverse proxy server. Create a new Nginx configuration file at /etc/nginx/sites-available/cache.conf.

upstream origin_server {
    server 127.0.0.1:8000;
}

server {
    listen 80;
    server_name _;

    location / {
        include proxy_params;
        proxy_pass http://origin_server;
    }
}

Notice how the upstream origin server is configured for the local IP address of 127.0.0.1 on port 8000. Recall that 8000 is the same port your website is listening on. Also take note that proxy_pass points to the origin server.

3. Configure Nginx Server Cache

With a basic reverse proxy server in place, let’s add in caching capabilities. Modify  cache.conf with the following highlighted changes.

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=custom_cache:10m inactive=60m;

upstream origin_server {
    server 127.0.0.1:8000;
}

server {
    listen 80;
    server_name _;

    location / {
        include proxy_params;
        proxy_pass http://origin_server;
        proxy_cache custom_cache;
        proxy_cache_valid any 10m;
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}

The first line containing proxy_cache_path defines the location of the Nginx cache on the system to be at /var/cache/nginx. If this directory doesn’t exist, it will be created the next time the Nginx server is reloaded.

Notice how the keys_zone value is custom_cache. We associate custom_cache  with our proxy_cache on line 14. For a full explanation of the arguments, please see the official Nginx docs.

In this example, all resources will be cached for a period of 10 minutes according to the proxy_cache_valid directive.

Finally, we add the caching status to the response header with the add_header directive.

4. Apply Your Configuration Changes

Make Nginx aware of your new configuration file by symbolic-linking it from the sites-available to the sites-enabled directory.

ln -s /etc/nginx/sites-available/cache.conf /etc/nginx/sites-enabled/

Apply your changes by reloading the Nginx web server.

systemctl reload nginx

5. Test Out Server-Level Cache

When you visit a page on your website for the first time, you will see a cache MISS in the response header. This means that the Nginx reverse proxy server did not find this page in its cache, so it went on to the origin server to dynamically generate the page.

Nginx server-level cache miss

The next time you visit the page, you will see a cache HIT in the response header. This means that the reverse proxy server found this page in its cache and served it directly without ever going to the origin server.

Nginx server-level cache hit

I have an entire video tutorial on Nginx server-level caching which will also walk you through these configuration steps in real-time. Please let me know if you have any questions.

YouTube video

Facebook
Twitter
Pinterest
LinkedIn
Reddit

Meet Tony

Tony from Tony Teaches Tech headshot

With a strong software engineering background, Tony is determined to demystify the web. Discover why Tony quit his job to pursue this mission. You can join the Tony Teaches Tech community here.

Leave a Reply

Your email address will not be published. Required fields are marked *