• 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 – Configure fastcgi_cache_valid for cache expiration

December 19, 2023

Nginx Tip – Configure fastcgi_cache_valid for cache expiration

Nginx is a popular web server and reverse proxy server that is known for its high performance and scalability. It is often used in conjunction with PHP to serve dynamic content efficiently. One of the key features of Nginx is its ability to cache dynamic content using the fastcgi_cache module. In this article, we will explore how to configure the fastcgi_cache_valid directive to control cache expiration.

Understanding fastcgi_cache_valid

The fastcgi_cache_valid directive is used to specify the cache expiration time for a particular response. It determines how long Nginx should keep a cached copy of a response before considering it stale and requesting a fresh copy from the backend server.

The syntax for the fastcgi_cache_valid directive is as follows:

fastcgi_cache_valid [code] time;

The [code] parameter is optional and allows you to specify a specific HTTP response code for which the cache validity should apply. If no response code is specified, the cache validity will apply to all response codes.

The time parameter specifies the cache validity period in seconds, minutes, hours, or days. For example, 10s represents 10 seconds, 5m represents 5 minutes, 2h represents 2 hours, and 1d represents 1 day.

Configuring fastcgi_cache_valid

To configure fastcgi_cache_valid, you need to add the directive within the relevant location block in your Nginx configuration file. Here’s an example:

location ~ .php$ {
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_cache my_cache;
    fastcgi_cache_valid 200 301 302 10m;
    fastcgi_cache_valid 404 1m;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
    include fastcgi_params;
}

In the above example, we have specified a cache validity of 10 minutes for HTTP response codes 200, 301, and 302. For response code 404, the cache validity is set to 1 minute. The fastcgi_cache_use_stale directive is used to specify which response codes should be served from the cache even if they are considered stale.

Benefits of Configuring fastcgi_cache_valid

Configuring fastcgi_cache_valid can provide several benefits:

  • Improved Performance: By caching dynamic content, Nginx can serve subsequent requests for the same content directly from the cache, reducing the load on the backend server and improving response times.
  • Reduced Server Load: Caching can significantly reduce the number of requests that need to be processed by the backend server, allowing it to handle more concurrent users and reducing server load.
  • Better Scalability: Caching can help improve the scalability of your application by reducing the resources required to generate dynamic content, allowing you to handle more traffic without the need for additional servers.

Conclusion

Configuring fastcgi_cache_valid is an effective way to control cache expiration in Nginx and improve the performance and scalability of your website. By specifying the cache validity period for different response codes, you can ensure that your cached content remains fresh and relevant. Take advantage of Nginx’s caching capabilities to optimize your website’s performance and provide a better user experience.

For more information about VPS hosting and how it can benefit your website, visit Server.HK.

Recent Posts

  • CentOS Server Performance Tuning: Optimization Techniques for 2026
  • How to Configure SELinux in CentOS Without Breaking Your System (CentOS Stream 9/10 – 2026)
  • Managing Users and Permissions in CentOS Stream: Best Practices (CentOS Stream 9/10 – 2026)
  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux

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