Member-only story
Nginx Fastcgi Reverse Proxy Cache for PHP (Symfony)
If you ever faced the case that your PHP app is not performing for the incoming traffic, Nginx can help you.
Actually there is a easy caching fix for PHP backends to reduce the load and to serve responses faster that are requested multiple times with the same parameters.
Current application state
In my case I had to fix a legacy API route that generated a JSON based response which took around 400ms. The app is using Memcached to cache serialized objects, but still the response time for an API should be much faster. The code itself was kind of legacy and a fast shot to speed up the application was requested.
Solution
The application lives behind a load balancer and runs on mulitple nodes with the following components:
- PHP-FPM as PHP Process Manager
- NGINX as webserver
The simplest solution that came to my mind was to cache the API responses, triggered by a POST request, even if the RFC for HTTP says its not recommended:
Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. However,the 303 (See Other) response can be used to direct the user agent to retrieve a…