This article may contain affiliate links. If you buy some products using those links, I may receive monetary benefits. See affiliate disclosure here
This blog is currently built with custom PHP and MySQL. So it was already loading fast even without any sort of full page caching. Unlike a heavy CMS like WordPress, which can usually take 200 to 300ms or even more to execute, this blog takes only 5-10ms on average to execute the PHP script.
But still I wanted to enable some form of full page caching to prevent PHP and MySQL queries getting executed unnecessarily. There is not much dynamic content, so a full page caching is worth it.
I had already heard that Nginx support page caching out of the box. You only need to enable it.
So, there were two options in front of me - one is Nginx FastCGI Cache and the other is Nginx Proxy Cache. And I decided to implement the latter. I will show you the steps to do that.
The scenario - Nginx FastCGI Cache vs Proxy Cache
As I said, this is site is built with PHP, MariaDB, and Nginx. These are running inside a docker container.
In addition to that, I have a second Nginx server working as a reverse proxy, which proxies requests to Docker locally.
So if I am about to implement Nginx FastCGI Cache, I've to do enable it in the Nginx running inside Docker. Because FastCGI Cache is meant to cache responses from a FastCGI process, that is PHP-FPM.
On the other hand, if I want to enable caching on the reverse proxy server, I have to use Nginx Proxy Cache. Because proxying is not a FastCGI request, instead it is an HTTP request. Proxy Cache is meant to cache HTTP responses from the upstream proxy server.
That's the difference.
How to Enable Nginx Proxy Cache
First I opened the /etc/nginx/nginx.conf
file, and added the proxy configuration to the http
block:
http {
# Define Proxy Cache
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=PROXY_CACHE:10m max_size=1g inactive=60m use_temp_path=off;
# other settings goes here....
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Here I am adding the proxy_cache_path
directive to the http
block, which sets the cache directory, cache size (1GB) and inactive time (1 hour).
Then I added the following in the site's server block, located at /etc/nginx/sites-available/coralnodes.com
server {
# Other settings...
location / {
proxy_pass http://localhost:8086;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Proxy Cache Rules
proxy_cache PROXY_CACHE;
proxy_cache_valid 200 1h; # Cache valid 200 responses for 1 hour
proxy_cache_valid 404 1m; # Cache 404 responses for 1 minute
proxy_cache_bypass $http_cache_control;
add_header X-Cache-Status $upstream_cache_status;
}
}
That's all! Then I restarted the nginx server, and it started working.
sudo systemctl restart nginx
Upon reloading pages, I could see the newly added header X-Cache-Status
.
Based on the Cache-Control Header, the value of X-Cache-Status can be HIT, MISS, BYPASS, or EXPIRED.
Next, I need to check the speed difference with and without Nginx Proxy Cache. Hope I can update the post with that details soon.