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:
- Nginx Proxy_Pass To Tomcat Nginx Proxy_Pass To Tomcat Introduction The Nginx Proxy_Pass directive is used to provide a secure and efficient way to redirect requests from a web server to a Tomcat Application Server.…
- Nginx Regex Location Cache File Ngnix Regex Location Cache File What is an Nginx Regex Location Cache File An Nginx regex location cache file is a type of configuration file used to make the web…
- Nginx Php Fpm 7.2 Nginx Php Fpm 7.2 Introduction to Nginx and PHP-FPM Nginx is an open-source web server, reverse proxy server, and load balancer. It is known for being lightweight and fast, and…
- How To Count Nginx Scalability How To Count Nginx Scalability What is Nginx? Nginx is an open source web server and reverse proxy server developed by Igor Sysoev. It is a high performance web server…
- Nginx Version For Php 7 Nginx Version For Php 7 What is Nginx? Nginx is a powerful, open source web server that is used to serve web applications and websites. It is fast, scalable, and…
- Configure Nginx As Proxy Server Configure Nginx As Proxy Server Introduction Nginx is a software application used for serving dynamic web pages and web content. It is an open source, lightweight and highly modular web…
- Optimize Nginx Php7.0-Fpm For High Load Optimize Nginx Php7.0-Fpm For High Load Enable Cache Control When dealing with high load on the server, you need to be sure that your web server is able to properly…
- Ow To Setup Nginx Url For Java How To Setup Nginx Url For Java What Is Nginx? Nginx is an open source HTTP server and reverse proxy software. It can be used to speed up web applications…
- Nginx 1.15.12 Http Server Found In Global Scan Data Passive Nginx 1.15.12 HTTP Server Found In Global Scan Data Passive What is Nginx? Nginx is a free, open-source web server created in 2004 by Russian developer Igor Sysoev. Nginx is…
- Nginx Location Multi Django Projects Nginx Location Multi Django Projects What are Django Projects and Multi Projects? Django projects are applications that are built on the Django web framework. They are composed of different components…
- Nginx No Need For Rest Api Django Rest Nginx No Need for Rest API Django Rest Introduction to Nginx Nginx is a open source web server created by Igor Sysoev and released in 2004. Nginx is known for…
- Nginx Robots.Txt Exclude From Caching Nginx Robots.Txt Exclude From Caching Caching is an important part of any website as it allows content to be delivered quickly and efficiently to its users. But, as with any…
- Nginx Access Log Max Size Nginx Access Log Max Size What is Nginx? Nginx (pronounced "engine x") is a free and open-source web server that is widely used for managing and proxying traffic. It was…
- 404 Not Found Htaccess Setting Nginx 1.10.3 Ubuntu 404 Not Found Htaccess Setting Nginx 1.10.3 Ubuntu What Is Htaccess Setting? The .htaccess file is a special file that is used by web servers to control how a website…
- Nginx Http Proxy Http 1.1 Nginx Http Proxy Http 1.1 What is Nginx Http Proxy? Nginx Http Proxy is an open-source web server used to serve web resources such as images, static files, and dynamic…
- How To Configure Nginx With Varnish How To Configure Nginx With Varnish Introduction Nginx and Varnish are powerful web server tools used to configure and optimize the delivery of web content. Nginx serves requests while Varnish…
- Nginx Config Proxy Pass Using Https Nginx Config Proxy Pass Using Https Introduction Nginx is an open source web server that contains robust and efficient config proxy pass feature for its users. It is designed to…
- Nginx Least Connections Load Balancing Nginx Least Connections Load Balancing What is Nginx? Nginx is an open-source web server which is popularly used for supporting high-traffic websites. It is known for its scalability, reliability, and…
- Nginx Proxy Cache Redis Wordpress Centos Nginx Proxy Cache Redis Wordpress Centos Understanding Nginx Proxy Cache Nginx Proxy Cache is a web-acceleration software from Nginx, Inc. It is designed to speed up dynamic web content delivery.…
- Nginx Wordpress Ubuntu 18.04 Nginx Wordpress Ubuntu 18.04 Introduction to Nginx Nginx is a web server software for hosting websites and applications. It is open-source and highly configurable, making it a popular choice for…
- Mac Os X Nginx Conf Location Mac OS X Nginx Conf Location What is Nginx? Nginx is a high-performance web server and reverse proxy originally written for Linux, but now it’s also available for Mac OS…
- Setup Nginx For Magento 2 Setup Nginx For Magento 2 What is Nginx? Nginx is a web server that was specifically created to serve dynamic web content efficiently. Its open-source code is freely available and…
- Default_Server Nginx Conf Digital Ocean Directory Default_Server Nginx Conf Digital Ocean Directory Introduction to Nginx & Digital Ocean Directory Nginx and Digital Ocean Directory make an excellent pair when it comes to setting up web servers.…
- Nginx Conf Sample Using Cache Load Balancer Nginx Conf Sample Using Cache Load Balancer Understanding Nginx and Load Balancing Nginx, an open source web server, has become a popular choice among web developers for its performance and…
- Googlec Cloud Hosting Nginx Caching Proxy Google Cloud Hosting Nginx Caching Proxy What is Nginx? Nginx is an open source web server typically used to serve high-traffic websites. It offers a powerful set of features and…
- Do We Need To Rebuild Nginx Docker If Change Default.Conf Do We Need To Rebuild Nginx Docker If Change Default.Conf? Understanding the Basics of Nginx Nginx is an open source web server software that is becoming increasingly popular as a…
- Nginx Proxy_Pass Multiple Locations Nginx Proxy_Pass Multiple Locations What is the Nginx Proxy_Pass Directive? The Nginx Proxy_Pass directive is a configuration setting in the Nginx web server software that allows the server to properly…
- Check Ok For Nginx Confgiruration On Ubuntu Check OK for Nginx Confgiruation On Ubuntu What is Nginx? Nginx is an open-source web server that is renowned for its scalability and agility. It was originally designed as an…
- 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 Convert Htaccess To Nginx How To Convert Htaccess To Nginx Understanding Htaccess And How It Works Htaccess is a configuration file that sits in the root directory and provides a set of instructions to…