FastCGI caching is an efficient method to cache dynamic content generated by PHP applications on your server. Learning how to set up FastCGI caching with Nginx enables you to leverage Nginx‘s FastCGI module, which provides a robust mechanism to cache PHP responses. This helps eliminate the overhead of repeatedly processing dynamic content, significantly improving server performance.
Step 1: Configure the Cache Path in Nginx
Start by editing the Nginx configuration file for your virtual host. This file is where you’ll enable the caching functionality. You can use any text editor such as vim or nano to open the virtual host file:
vim /etc/nginx/sites-enabled/virtual_host.conf
Add the following cache path configuration at the top of the file (but outside the server block):
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_path: Defines the location of the cache directory (/var/cache/nginx), its memory size (100m), the memory zone name (MYAPP), the subdirectory levels for organization (1:2), and the cache expiration (inactive=60m means cached content will be purged after 60 minutes of inactivity).
fastcgi_cache_key: Specifies the key used to name the cached files. This will be based on the request’s scheme (HTTP/HTTPS), method (GET, POST, etc.), host, and request URI.
Step 2: Configure the PHP Location Block for FastCGI Caching
Next, locate the location ~ \.php$ block in your Nginx configuration, where PHP requests are handled. Add the following lines inside the location block:
fastcgi_cache MYAPP;
fastcgi_cache_valid 200 60m;
This configuration tells Nginx to use the MYAPP cache zone and cache PHP responses that return a 200 OK status for 60 minutes. The fastcgi_cache_valid directive specifies the duration for which a response with status code 200 should be cached.
Step 3: Test Your Configuration
Before reloading Nginx, you should test your configuration for errors. Run the following command to verify:
nginx -t
If there are no errors, reload Nginx to apply the changes:
systemctl restart nginx
Step 4: Test FastCGI Caching
To verify that the caching is working correctly, create a PHP file that outputs a UNIX timestamp:
<?php
echo time();
?>
Save this file as /path_to_document_root/echotime.php.
Now, request this file multiple times using curl or your browser. If caching is working, you should receive the same timestamp for each request, as the response is being served from the cache instead of being generated dynamically by PHP.
Example of repeated curl requests:
root@server:~# curl https://domain_name/echotime.php
1688009643
root@server:~# curl https://domain_name/echotime.php
1688009643
root@server:~# curl https://domain_name/echotime.php
1688009643
Step 5: Verify Cache Storage
You can check the cached files stored by Nginx by listing the contents of the cache directory:
root@server:~# ls -lR /var/cache/nginx/
You should see cached files inside subdirectories. Each file in the cache directory is named based on the hash of the cache key.
Step 6: Add a Cache Status Header
To easily check whether a cached response is served, you can add an X-Cache header to the response. This will indicate whether the content was served from the cache or generated dynamically.
Add the following line above the server block:
add_header X-Cache $upstream_cache_status;
Reload Nginx again:
nginx -t
systemctl restart nginx
Now, when you make a request, you can inspect the headers using curl -v:
root@server:~# curl -v https://domain_name/echotime.php
In the response, the X-Cache: HIT header indicates that the response was served from the cache. If the cache is not hit, you will see X-Cache: MISS.
Conclusion
With FastCGI caching enabled on Nginx, you can significantly improve the performance of your PHP-based websites by reducing the load on your PHP backend. This setup is ideal for content that doesn’t change frequently, helping to reduce server resource usage and speed up response times for users. To achieve this, follow the steps in How to Set Up FastCGI Caching with Nginx and optimize your website’s efficiency.
If you have any questions or need assistance while setting up FastCGI caching with Nginx, don’t hesitate to contact our support team. We’re here to help with any issues related to how to set up FastCGI caching with Nginx and ensure your website runs smoothly. Our server management services are available to provide expert guidance and troubleshoot any challenges you may encounter during the setup process. Reach out to us for personalized support and make the most of your Nginx server configuration!