Sylius的Nginx配置

最近,我们开始将越来越流行的Sylius平台用于我们的一个电子商务项目。 Sylius基于Symfony2,Symfony2是我们在摄影网站上使用的领先的PHP框架。即使我们将WooCommerce用于较小的项目,Sylius也提供了更现代,更强大的解决方案,我们相信绝对更适合于雄心勃勃的电子商务网站。

我们最初遇到的问题是让Sylius在Nginx + PHP-FPM + Ubuntu 14.04 Web服务器上正常工作。我们从标准的Symfony2配置文件开始,并进行了一些优化,但是无法使任何产品缩略图起作用。事实证明, LiipImagineBundle用于即时生成缩略图,这干扰了我们的缓存过期规则集,导致我们尝试显示的任何图像缩略图都出现404。

这是我们错误地用来将静态文件的到期日设置为1年的原因:

location ~ \.(js|css|png|jpeg|jpg|gif|ico|swf|flv|pdf|zip)$ { # Set expiry date to 1 year in the future. expires 365d ; }
代码语言: Nginx nginx

尽管这通常可以正常工作,但这也意味着任何以这些扩展名之一结尾的URL都将完全绕过app.php文件,而不管实际的图像文件是否存在。由于Sylius中的缩略图是根据URL请求即时创建的,因此完全中断了缩略图的创建。

解决方法非常简单:我们只为实际存在的请求文件设置有效期限。否则,我们照常将它们重写为app.php文件。这是我们使用的最终Nginx配置文件:

server { server_name example.com; root /var/www/example.com/web; location / { try_files $uri @rewriteapp ; # Redirect to app.php if the requested file does not exist. } # Development rule-set. # This rule should only be placed on your development environment. # In production, don't include this and don't deploy app_dev.php or config.php. location ~ ^/(app_dev|config)\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$ ; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ; fastcgi_param HTTPS off ; } # Production rule-set. location ~ ^/app\.php(/|$) { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$ ; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root $fastcgi_script_name ; fastcgi_param HTTPS off ; # Prevents URIs that include the front controller. This will 404: # http://domain.tld/app.php/some-path # Remove the internal directive to allow URIs like this. internal; } # Static files rule-set. location ~ \.(js|css|png|jpeg|jpg|gif|ico|swf|flv|pdf|zip)$ { # Set rules only if the file actually exists. if (-f $request_filename ) { # Set expiry date to 1 year in the future. expires 365d ; # Further optimize by not logging access to these files. access_log off ; } # Rewrite to app.php if the requested file does not exist. try_files $uri @rewriteapp ; } # Rewrite rule for PHP files. location @rewriteapp { rewrite ^(.*)$ /app.php/ $1 last ; } error_log /var/log/nginx/example.com_error.log; access_log /var/log/nginx/example.com_access.log; }
代码语言: Nginx nginx

而已。现在可以即时动态生成缩略图,而现有图像也使用正确的到期标头。让我们知道这是否对您有用,或者您是否使用了其他方法。祝您编码愉快!