• 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 Tip – Use the proxy_cache_path directive for cache path setup

December 19, 2023

Nginx Tip – Use the proxy_cache_path directive for cache path setup

When it comes to optimizing website performance, caching plays a crucial role. By caching frequently accessed content, you can significantly reduce server load and improve response times for your visitors. Nginx, a popular web server and reverse proxy server, offers a powerful caching feature through the use of the proxy_cache_path directive.

What is the proxy_cache_path directive?

The proxy_cache_path directive is used in Nginx to define the location and settings for the cache storage. It allows you to specify the path where cached files will be stored on the server’s disk, as well as various cache-related parameters.

By utilizing the proxy_cache_path directive, you can enable caching for specific locations or entire websites, improving the overall performance and reducing the load on your server.

Setting up the proxy_cache_path directive

To set up the proxy_cache_path directive, you need to modify your Nginx configuration file. Here’s an example of how to do it:

http {
  proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
  
  server {
    ...
    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;
      ...
    }
    ...
  }
}

In the above example, we define the cache path using the proxy_cache_path directive. The /path/to/cache specifies the location on the server where the cached files will be stored. The levels=1:2 parameter determines the directory structure of the cache, while keys_zone=my_cache:10m sets up a shared memory zone for caching.

The max_size=10g parameter specifies the maximum size of the cache, which in this case is set to 10 gigabytes. The inactive=60m parameter determines how long an inactive cache item will be kept before it is considered expired. Finally, use_temp_path=off disables the use of temporary files for caching.

Within the server block, we enable caching for the desired location using the proxy_cache directive. The proxy_cache_valid directive specifies the caching duration for different HTTP response codes. In this example, responses with a 200 or 302 status code will be cached for 10 minutes, while a 404 status code will be cached for 1 minute.

The proxy_cache_use_stale directive determines which types of errors should be served from the cache if the backend server is unavailable or experiencing issues.

Benefits of using the proxy_cache_path directive

By utilizing the proxy_cache_path directive in Nginx, you can enjoy several benefits:

  • Improved performance: Caching frequently accessed content reduces the load on your server and improves response times for visitors.
  • Reduced bandwidth usage: Cached content is served directly from the server’s disk, reducing the need for data transfer from the backend server.
  • Increased scalability: Caching allows your server to handle more concurrent requests without sacrificing performance.
  • Better user experience: Faster page load times lead to a better overall user experience and increased visitor satisfaction.

Conclusion

The proxy_cache_path directive in Nginx is a powerful tool for optimizing website performance through caching. By properly configuring the cache path and related parameters, you can significantly improve response times, reduce server load, and enhance the overall user experience. Take advantage of this feature to maximize the benefits of your VPS hosting environment.

For more information on VPS hosting solutions, visit Server.HK.

Recent Posts

  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

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