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 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.…
- Reverse Proxies Nginx Centos 6 Reverse Proxies Nginx Centos 6 What is a Reverse Proxy? A reverse proxy is a type of proxy server that retrieves resources on behalf of a client from one or…
- 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…
- 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 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…
- Enable Memcached Nginx Debian 9 Enable Memcached Nginx Debian 9 Memcached Basics Memcached is a distributed memory caching system that greatly enhances the performance of network-based applications. It is a great tool that allows data…
- Reverse Proxy Nginx Dan Windows Server Reverse Proxy Nginx Dan Windows Server Introduction to Reverse Proxy Reverse proxy is a server that is used to receive requests from the Internet and forward them to other servers.…
- 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…
- Speed Up Vestacp On Centos 7 Nginx Apache Speed Up VestaCP On Centos 7 Nginx Apache Overview of the Setup VestaCP is a popular hosting control panel that uses a set of open source servers and applications to…
- Nginx Can't Access Folder Forbidden Nginx Can't Access Folder Forbidden What is Nginx? Nginx is a web server that is developed to serve static content quickly and efficiently. It is an open source, popular web…
- 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…
- 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 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 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…
- 404 Not Found Nginx 1.10 1 404 Not Found Nginx 1.10 1 What Is Nginx 1.10 1? Nginx 1.10 1 is a web server software that was released on July 1, 2017. It is the latest…
- What Is Reverse Proxy Nginx What Is Reverse Proxy Nginx? What is Nginx? Nginx is a free, open-source web server software developed by Igor Sysoev since 2002. It gained immense popularity due to its ability…
- Centos 7 Nginx Multiple Websites Centos 7 Nginx Multiple Websites Introduction Are you looking for a way to set up multiple websites on your CentOS 7 server utilizing the Nginx web server? If so, you’ve…
- Nginx Reverse Proxy Etc Default Nginx Reverse Proxy Etc Default What is a Nginx Reverse Proxy? An Nginx reverse proxy is a type of web server that relays client requests from one server to one…
- 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 Cache Control For Static Files Nginx Cache Control for Static Files What is Nginx? Nginx (pronounced engine-x) is an open source web server and web application platform. It was created by Russian developer Igor Sysoev…
- 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…
- Nginx Vs Express Whats The Difference Nginx Vs Express: What's The Difference? The History of Nginx and Express Nginx is an open-source web server that has been around for over twelve years. It was created in…
- Nginx 502 Bad Gateway Php Wordpress Digitalocean Nginx 502 Bad Gateway Php Wordpress Digitalocean Introduction to 502 Bad Gateway A 502 Bad Gateway is an HTTP status code that is displayed when the server acting as a…
- 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…
- Nginx Caching Static Files Using Mime Image Nginx Caching Static Files Using Mime Image Introduction to MIME Image Caching MIME (Multipurpose Internet Mail Extensions) images are those images used by the browsers and email clients for email…
- Install Nginx Di Whm Domainesia Install Nginx Di Whm Domainesia Install Nginx Di Whm Domainesia Introduction to Nginx Nginx (engine x) is a popular open-source, high-performance web server written in C. Its main goal is…
- What Is The Difference Between Uwsgi And Nginx What Is The Difference Between Uwsgi And Nginx? What Is Uwsgi? Uwsgi stands for “Unicorn web server gateway interface”. It is a powerful, highly configurable and widely used web server…
- Ubuntu Nginx See Real Time Request Ubuntu Nginx - See Real Time Requests Introduction to Nginx on Ubuntu OS Nginx is a powerful, open source web server software that runs in the background of an Ubuntu…
- 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…
- Nginx Robots Exclude From Caching Nginx Robots Exclude From Caching What is Caching? Caching is one of the most important tools when it comes to website performance. Caching allows web servers to store a copy…