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.
Related Posts:
- How To Run Service Nginx Docker How To Run Service Nginx Docker What is Nginx? Nginx is an open-source web server and reverse proxy created by Igor Sysoev in 2002. It has gained widespread popularity due…
- How To Disable Nginx On Plesk Nginx How To Disable Nginx On Plesk Nginx What Is Nginx? Nginx is a web server and reverse proxy application that's similar to Apache but faster and more optimized for high…
- Nginx Reverse Proxy Passthrough Ssl Nginx Reverse Proxy Passthrough SSL Overview Nginx Reverse Proxy Passthrough SSL (Secure Sockets Layer) is a technique that enables you to securely access backend resources by using a reverse proxy.…
- Nginx Php-Fpm Php Stack Overflow Articel Nginx Php-Fpm Php Stack Overflow Articel What Is Nginx? Nginx (pronounced engine-x) is an open source web server and reverse proxy software that is popular for its high performance and…
- Docker Compose Nginx Node Js Mysql Docker Compose Nginx Node Js Mysql What is Docker Compose? Docker Compose is a utility used to deploy and manage applications created with multiple services (or containers) in a single…
- Install And Configure Nginx Mysql Install and Configure Nginx Mysql What is Nginx Nginx is an open source, high-performance HTTP server, reverse proxy, and IMAP/POP3 proxy server. It provides load balancing, content caching, access control,…
- Nginx Cache Base On Mime Nginx Cache Base On Mime What is Nginx Cache based On Mime? NginxCache based on MIME is a powerful caching system that enables web servers to improve the performance of…
- Nginx Forward To Another Url Nginx Forward To Another Url Introduction to Nginx Nginx is a web server similar to Apache. It is the backbone of web servers that power modern websites around the world.…
- This Site Can't Be Reached Nginx This Site Can't Be Reached Nginx What is Nginx and What Does it do? Nginx is an open-source web server, created by Igor Sysoev in 2004. It is a powerful…
- Linux Nginx Load Balancer Memory Requirement Linux Nginx Load Balancer Memory Requirement Introduction to Linux Nginx Load Balancer Linux Nginx Load Balancing is an extremely powerful, reliable and efficient method for hosting multiple websites on the…
- Laravel 5.5 Configure Nginx Laravel 5.5 Configure Nginx Introduction To Nginx Nginx is a web server software often deployed as a reverse proxy. It is open-source and available to download for free. Nginx has…
- How To Install Php 5 Nginx Centos How To Install Php 5 Nginx Centos What Is PHP 5 Nginx Centos? PHP 5 Nginx Centos is an open source web server software that is optimised to serve dynamic…
- Making Nginx Runs As User Making Nginx Runs As User What is Nginx? Nginx (pronounced "Engine-X") is a popular web server software. It is open-source and can be configured to run on many operating systems,…
- Php-Fpm Cache Nginx Centos Php-Fpm Cache Nginx Centos Introduction to Nginx, Php-Fpm, and Centos Nginx, PHP-FPM, and Centos are three powerful, open-source technologies that are used to create powerful applications, websites, and services. Nginx…
- How To Use Nginx Laravel Laragon How To Use Nginx Laravel Laragon Introduction Laragon is a powerful, lightweight, robust web server stack that is used to develop and host applications on Windows and Linux. Laragon uses…
- Nginx Read Php Files Outside Root Nginx Read Php Files Outside Root Understanding the Basics of Nginx Nginx is an open source web server and HTTP proxy server originally developed by Igor Sysoev. It can be…
- Reverse Engine Nginx Dan Windows Server Reverse Engine Nginx and Windows Server Why Use a Reverse Proxy on Windows? Reverse proxying is a process to allow for easier access to a certain site over the Internet.…
- How To Set Static Nginx How To Set Static Nginx Understanding What is Nginx? Nginx is an open source Web server software used for hosting static or dynamic websites, media streaming, and other web applications.…
- Nginx Conf Serve Static Files Nginx Conf Serve Static Files What is Nginx? Nginx is an open source web server created in 2004 by Igor Sysoev. It’s fast and reliable, making it an ideal web…
- Nginx Proxy_Cache_Bypass Cookie Nginx Proxy_Cache_Bypass Cookie What is a Proxy_Cache_Bypass Cookie? A Proxy_Cache_Bypass Cookie is a special kind of cookie used to instruct a proxy server to bypass its own caching process. This…
- How To Install Nginx With Varnish Webuzo How To Install Nginx With Varnish Webuzo What is Nginx? Nginx is an open source web server created by Igor Sysoev in 2004. It is a light-weight, robust, high performance…
- Stup Nginx In A Domain Medium Steps to Setup Nginx In A Domain Medium Introduction To Nginx Nginx (pronounced engine-x) is a lightweight, open source web server that was originally designed as a proxy server for…
- Certbox Nginx Not Showing My Domain Certbot Nginx Not Showing My Domain What Is Nginx and How Does It Work? Nginx is an open source web server software that is often used as a reverse proxy,…
- Hide Html Extension On Nginx Disclaimer - This article is for informational purposes only. The author does not make any representations or warranties as to accuracy, completeness, or the results obtained from any information provided.…
- Install Reverse Proxy Nginx Linux Virtualbox Install Reverse Proxy Nginx Linux Virtualbox What is a Reverse Proxy? A reverse proxy is a type of server that takes requests from the Internet and forwards them to backend…
- Nginx More Than 4 Config Nginx More Than 4 Config Basics of Nginx Nginx is a powerful, open source web server. It is designed to be both efficient and secure. It is used to animate…
- Nginx Reverse Proxy Connection Refused Nginx Reverse Proxy Connection Refused What is Nginx? Nginx is a powerful web server and reverse proxy server with a wide range of features and applications. Nginx allows you to…
- Wordpress Performance Easy Engine Nginx Wordpress Performance Easy Engine Nginx Introduction to WordPress and Performance WordPress is a popular, open-source content management system (CMS) often used for creating professional websites. This platform has easy-to-use features…
- Nginx Conf Test Failed Permission Denied Nginx Conf Test Failed Permission Denied What Is Nginx? Nginx is an open source web server software that runs on Linux, Windows, BSD, and Mac OSX. The software is robust…
- Nginx How To Override Nginx.Conf Conf.D Nginx How To Override Nginx.Conf Conf.D What is Nginx? Nginx is a high-performance web server that is commonly used for web hosting and reverse proxies. It has become increasingly popular…