Nginx Conf Sample Using Cache


Nginx Conf Sample Using Cache

Introduction

Caching is an essential technique for website performance optimization. It helps to reduce server workload, enable scalability and serve contents faster. One of the most important configurations for web server performance is the Nginx configuration for caching. Nginx, short for “engine x”, is a free, open source web server that can serve web content more efficiently than most web servers. In this article we will define a generic Nginx configuration for caching, and provide several variations for different use cases.

Setting Up Nginx for Caching

To set up Nginx for caching, it is important to understand how caching works in Nginx. Nginx works with two cache levels: file and memory. File caching is mostly used for static content (images, HTML, CSS, JavaScript, etc.). Memory caching is used for dynamic content, such as PHP and other scripting language outputs. In Nginx, each request is first served from file cache, and if not found there it is served from memory cache, and if still not found, from the origin server.

In order to use caching in Nginx, you must first enable the proper module: nginx-cache-purge. Then you need to add a few directives to your Nginx configuration. A generic caching configuration would look something like this:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:128m max_size=20g inactive=14d; server {…} # This is where you add your other directives
proxy_cache_key “$scheme$request_method$host$request_uri”;
proxy_cache my_cache;
proxy_cache_valid 14d;

The proxy_cache_path directive specifies the directory where Nginx will store cached contents, and the size and duration of the cache entries. The keys_zone directive specifies the name of the cache (my_cache in this example) and the amount of memory to use for the cache. The max_size directive specifies the maximum size of the cache on disk. The inactive directive specifies the expiration time for cached entries, which are inactive for more than the specified time.

In this configuration, Nginx will cache all requests that get a valid response from the origin server (HTTP status code 200 OK). Requests to resources that generate a 3xx redirect or a 4xx or 5xx error will not be cached.

Caching Static Content

The most common use case for web server caching is to cache static content, such as images, HTML, CSS, JavaScript, etc. To cache static content in Nginx, you must first add the following directives to your Nginx configuration file:

server {
location ~* .(css|js|gif|jpe?g|png)$ {
expires 30d;
proxy_cache my_cache;
proxy_ignore_headers Expires Cache-Control Set-Cookie;
proxy_cache_use_stale error timeout invalid_header http_500;
proxy_cache_valid 200 1d;
}
}

The location directive contains a regular expression that matches all requests for static content, such as images, HTML, CSS, and JavaScript. The expires directive sets the expiration time for the content (in this case, 30 days from the time it was last modified). The proxy_cache directive defines the cache that will be used for the content, and the proxy_ignore_headers directive instructs Nginx to ignore any expiration and caching headers sent by the origin server. The proxy_cache_use_stale directive enables Nginx to serve stale (previously cached) content when the origin server is not responding. The proxy_cache_valid directive sets the expiration time for cached content (in this case 1 day).

Caching Dynamic Content

Caching dynamic content (such as PHP output) can be more complex than caching static content. To cache dynamic content, you must use Nginx’s memory caching. To configure memory caching in Nginx, add the following directives to your configuration file:

location ~ .php$ {
proxy_cache_key “$host$request_uri$cookie_email”;
proxy_cache my_cache;
proxy_cache_valid 200 1m;
proxy_cache_methods GET; }

The proxy_cache_key directive defines the key that will be used for caching. This key should include information about the current request, such as the host, the request URI and any cookies. The proxy_cache_valid directive sets the expiration time for cached content (in this case 1 minute). The proxy_cache_methods directive sets the HTTP methods eligible for caching (in this case just GET).

Cache Invalidation

One of the most difficult aspects of caching is keeping the cache up to date with changes made to the origin server. In Nginx, there are several methods for cache invalidation. The most commonly used is the “cache purge” method. To enable cache purging in Nginx, you must add the following directive to the Nginx configuration file:

proxy_cache_purge purge PUT “$scheme$request_method$host$request_uri”;

This directive enables Nginx to purge the cache when it receives a PUT request. In this example, the request must include a valid URL (the host, request URI and scheme) in order for the cache to be purged. Nginx also supports other methods for cache invalidation, such as “if modified” and “stale” methods.

Conclusion

Caching is an essential tool for improving web server performance. Configuring Nginx for caching can be complex, but with a few simple configuration directives, it is possible to enable caching for both static and dynamic content. Once configured, Nginx can cache content from the origin server and serve it faster with fewer requests.

FAQs

  • Q. What is Nginx?

    A. Nginx is a free, open source web server that can serve web content more efficiently than most web servers.

  • Q. What is caching?

    A. Caching is a technique used to temporary store requested data, so that a subsequent request for the same data can be served faster.

  • Q. What is cache invalidation?

    A. Cache invalidation is the process of removing old or invalid cached content from the cache so that it can be updated with new or valid data from the origin server.

Thank you for reading this article. Please read other articles for more information.

Leave a Reply

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