• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

Nginx for Newbie: set up caching

December 18, 2023

Nginx for Newbies: Set Up Caching

When it comes to web servers, Nginx is a popular choice due to its high performance and scalability. One of the key features that make Nginx stand out is its ability to handle caching efficiently. In this article, we will explore how to set up caching with Nginx for beginners.

What is Caching?

Caching is the process of storing frequently accessed data in a temporary storage location, known as a cache. When a user requests a resource, the server checks if it is available in the cache. If it is, the server serves the cached version, which significantly reduces the response time and improves overall performance.

Why Use Caching?

Caching offers several benefits for website owners and users:

  • Improved Performance: Caching reduces the load on the server and decreases the time it takes to deliver content to users.
  • Better User Experience: Faster load times lead to a better user experience, resulting in higher user satisfaction and engagement.
  • Reduced Server Load: By serving cached content, the server can handle more requests without being overwhelmed.
  • Bandwidth Savings: Caching reduces the amount of data transferred between the server and the user, resulting in lower bandwidth usage and cost savings.

Setting Up Caching with Nginx

Before setting up caching, ensure that Nginx is installed and configured correctly on your server. Once that is done, follow these steps:

Step 1: Configuring the Cache Path

The first step is to define the location where Nginx will store the cached files. Open the Nginx configuration file (usually located at /etc/nginx/nginx.conf) and add the following line within the http block:

proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

Replace /path/to/cache with the desired location on your server where you want to store the cache files. The levels parameter determines the directory structure of the cache, while keys_zone sets the name and size of the cache zone. Adjust the values according to your requirements.

Step 2: Enabling Caching for Specific Locations

Next, you need to specify which resources should be cached. Open your Nginx configuration file again and add the following lines within the server block:

location / {
    proxy_cache my_cache;
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
    proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
    proxy_cache_background_update on;
    proxy_cache_lock on;
    proxy_cache_lock_timeout 5s;
    proxy_ignore_headers Cache-Control;
    proxy_ignore_headers Set-Cookie;
    proxy_hide_header Set-Cookie;
}

This configuration enables caching for the root location (“/”) of your website. Adjust the location and cache settings as per your requirements. The proxy_cache_valid directive specifies the caching duration for different HTTP response codes. The proxy_cache_use_stale directive allows Nginx to serve stale content if the backend server is unavailable. The other directives control various caching behaviors.

Step 3: Testing and Verifying Caching

After making the necessary changes, save the configuration file and restart Nginx to apply the changes. You can now test if caching is working correctly by accessing your website and checking the response headers. Look for the “X-Cache” header, which indicates whether the content was served from the cache or not.

Summary

Nginx’s caching capabilities can significantly improve the performance and scalability of your website. By following the steps outlined in this article, you can set up caching with Nginx and enjoy the benefits of faster load times, reduced server load, and improved user experience.

To learn more about VPS hosting and how it can enhance your website’s performance, visit Server.HK.

Recent Posts

  • How to Migrate Your Website to a Hong Kong VPS: Zero-Downtime Transfer Guide (2026)
  • How to Set Up Redis on Hong Kong VPS: Caching, Queues, and Session Storage (2026)
  • How to Host a Python Flask or Django Application on Hong Kong VPS (2026)
  • How to Set Up WireGuard VPN on a Hong Kong VPS: Step-by-Step Guide 2026
  • Hong Kong VPS vs DigitalOcean: Cost, Performance, and China Routing Compared (2026)

Recent Comments

  1. vibramycin injection on How to Choose the Right Hong Kong VPS Plan: A Buyer’s Guide for 2026
  2. allopurinol for gout on CN2 GIA vs BGP vs CN2 GT: What’s the Real Difference for China Connectivity?
  3. antibiotics online purchase on How to Set Up a WordPress Site on a Hong Kong VPS with aaPanel (Step-by-Step 2026)
  4. linezolid cost oral on Top 5 Use Cases for a Hong Kong Dedicated Server in 2026
  5. metoprolol generic on Hong Kong VPS vs Japan VPS: Head-to-Head for Asia-Pacific Deployments in 2026

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot