Optimizing Magento Performance: Nginx + PHP-FPM + FastCGI Cache Configuration
Introduction
The recent “Optimizing Magento For Peak Performance” whitepaper released by Magento provides valuable insights for enhancing the performance of your e-commerce platform. While targeted at Enterprise Edition users, many of these tips can also be applied to the Community edition with some adjustments.
Merging Recommended Configurations
After studying the whitepaper, I decided to merge their suggested Nginx + fastcgi/proxy cache configuration with my standard virtual host setup for Magento and made a few minor improvements. Here’s the resulting configuration:
fastcgi_cache_path /tmp/fcgi levels=1:2 keys_zone=MAGE:64m max_size=128m inactive=10h;
server {
listen 99999; ## Nginx port
server_name domain.com www.domain.com;
root /www/magento; ## App folder
index index.php;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
access_log off;
log_not_found off;
}
location /index {
try_files $uri @fcgi_nocache;
}
location /checkout {
try_files $uri @fcgi_nocache;
}
location / {
try_files $uri @fcgi_cache;
if ($cookie_frontend) { return 413; }
if ($cookie_CUSTOMER_AUTH) { return 413; }
if ($request_method = POST ) { return 413; }
error_page 413 = @fcgi_nocache;
}
# Deny access to hidden files
location ~ (/(app/|includes/|/pkginfo/|var/|report/config.xml)|/\.svn/|/.hta.+) {
deny all;
}
# Forward paths like /js/index.php/x.js to relevant handler
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
# Manually purge pages
location ~ /purge(/.*) {
fastcgi_cache_purge MAGE "$scheme$request_method$host$1";
}
location @fcgi_cache {
#if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
fastcgi_pass unix:/var/spool/phpfpm.sock; ## php-fpm socket
include fastcgi_params;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 60;
fastcgi_read_timeout 60;
#... (remaining config)
}
location @fcgi_nocache {
#if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
fastcgi_pass unix:/var/spool/phpfpm.sock; ## php-fpm socket
include fastcgi_params;
#... (remaining config)
}
}
Understanding the Configuration
FastCGI Cache vs. FastCGI No-Cache
The @fcgi_cache and @fcgi_nocache blocks are used to define separate behavior for cached and non-cached requests.
- The
location /block serves most pages using the cache (@fcgi_cache), except for specific paths like/indexor/checkout, which bypass the cache directly (@fcgi_nocache).
Cookie-Based Caching Control
The configuration uses cookies to control caching:
- If certain cookies are present (e.g.,
X-Store=1), Nginx serves cached pages. - Cookies are set for anonymous users who don’t have authentication cookies.
Performance Analysis and Questions
After testing this setup using Apache Benchmark (AB), the results appear promising. However, I’m uncertain about the accuracy of these tests and whether the cache system is functioning as expected. Some questions remain:
- What is the actual logic behind
@fcgi_cacheand@fcgi_nocache? Which users are receiving cached pages? - The stale cache doesn’t seem to work properly when PHP-FPM is turned off.
- I’m confused by the different headers being sent, especially regarding cache status.
Suggestions for Further Investigation
To better understand this configuration:
- Monitor Nginx access and error logs to observe cache hits and misses.
- Analyze Magento’s performance using tools like New Relic or Blackfire for precise metrics on PHP execution time and database queries.
- Test the configuration under varying loads, such as simulated user traffic patterns.
Remember that optimizing Magento performance requires a multi-layered approach, including database optimization, proper indexing, and careful choice of extensions. This Nginx configuration is just one piece of the puzzle.
Conclusion
By merging the recommended Nginx + PHP-FPM + FastCGI cache configurations with your existing setup, you can significantly enhance Magento’s performance. However, thorough testing and monitoring are essential to ensure optimal results.
If you need further assistance in configuring or tuning your Magento environment, feel free to reach out to our team of dedicated developers at LaravelCompany.com. We specialize in providing high-quality solutions for e-commerce platforms like Magento.