Nginx configuration for Sylius

Recently we started using the increasingly popular Sylius platform for one of our e-commerce projects. Sylius is based on Symfony2, the leading PHP framework which we have worked with on our photography website. Even though we use WooCommerce for smaller projects, Sylius provides a more modern and powerful solution that we believe is definitely better suited for more ambitious e-commerce websites.

A problem that we initially struggled with was getting Sylius to work properly on our Nginx + PHP-FPM + Ubuntu 14.04 web server. We started with a standard Symfony2 configuration file plus some optimizations, but could not get any product thumbnails to work. Turns out that LiipImagineBundle is used to generate thumbnails on-the-fly, and this was interfering with our cache expiry rule-set, resulting in 404s for any image thumbnails that we tried to display.

Here is what we were incorrectly using to set an expiry date of 1 year in the future for static files:

While this normally works without a problem, it also means that any URL ending with one of those extensions will completely bypass the app.php file, whether or not the actual image file exists. Since thumbnails in Sylius are created on-the-fly upon a URL request, this completely breaks thumbnail creation.

The fix is very straightforward: We only set the expiry date for requested files that actually exist. Otherwise, we rewrite them to the app.php file, as usual. Here is the final Nginx configuration file that we used:

That’s it. The thumbnails are now being properly generated on-the-fly, while the existing images are also using proper expiry headers. Let us know if this worked for you, or if you used a different approach. Happy coding!