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:
- Install Nginx Centos 7 Offline Install Nginx Centos 7 Offline Introduction Nginx is an open-source web server software, which is used to serve web pages and associated content. It is a fast, reliable, and robust…
- 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 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…
- 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.…
- Proxy Pass To Ip Public Nginx Proxy Pass To Ip Public Nginx What Is Nginx Nginx is a web server that is used to serve webpages and content on the internet. It is known for its…
- Haproxy Nginx X-Forwarded-For Haproxy Nginx X-Forwarded-For What is Haproxy and Nginx? Haproxy and Nginx are two web servers commonly used for load-balancing and hosting websites. Haproxy is a high performance reverse proxy that…
- 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…
- 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 Add Cache Control Header Nginx Add Cache Control Header 1. What is Cache Control Header? Cache Control Header is a type of header that is used to manipulate the browser cache by setting various…
- 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…
- 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 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 Proxy To Port 8080 Nginx Proxy To Port 8080 What is Nginx? Nginx is an open-source, high-performance web server developed in 2002 by Igor Sysoev and released publicly in 2004. It is a very…
- 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 Tuning For Best Performance Nginx Tuning For Best Performance What is Nginx? Nginx (pronounced "engine x") is an open source web server and reverse proxy server for HTTP, SMTP, POP3 and IMAP protocols. It…
- 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…
- 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…
- 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…
- 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…
- 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…
- 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…
- Php Share Memory Php Nginx PHP Share Memory & Nginx What is Share Memory in PHP? Share memory in PHP is a system that stores and retrieves data in memory, as opposed to storing data…
- 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 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…
- Compress Components With Gzip Nginx Compress Components With Gzip Nginx What is Compression? Compression is a process of reducing the size of data between server and user. It saves the amount of internet bandwidth wasted…
- 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…
- 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…
- 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…
- 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…
- Nginx Proxy Based On Location Nginx Proxy Based On Location Overview of Nginx Proxy Server Nginx is an open-source web server and proxy server created by Igor Sysoev. It has been one of the most…