Liip-imagine-bundle 在 Nignx 无法生成缓存图的问题

原因图片是在访问cache时候生成的,但是用nginx默认的rewrite rule 会有问题,要额外加一条:

location ~ ^/uploads/cache {
    try_files $uri @rewriteapp;
}

In this blog post, I will expose you how you should configure your Nginx in order to make it working the Avalanche Imagine Bundle.

This bundle is a great peace of code which allows to really ease backend images processing. For example, you can generate a new thumbnail of a picture which will be created on the fly if it does not exist or will be simply returned otherwise by simply defining a configuration node :)

To come back on the main topic, my first Nginx config try was the following (I have omit all optimization except the one related to the bundle)

server {
    root /var/www/current/web;
    server_name my-project.com;

    location / {
        try_files $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$ /app.php/$1 last;
    }

    location ~ ^/(app|app_dev|app_test)\.php(/|$) {
        include fastcgi_params;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|svg|ttf|woff)$ {
        expires 1y;
    }}

Here, the main point is I want to enable the Nginx HTTP header cache for all front-end assets. For that, I have added the last location instruction which adds an expiration header of one year. The issue with this configuration is the main instruction (ie. location /) does not match the assets anymore and so, are not processed by PHP-FPM causing no assets generations on the fly

To fix it, we need to rewrite the URI if the original one does not exist. For example, if your bundle cache assets directory is web/uploads/cache, then, we need to add this instruction to Nginx:

location ~ ^/uploads/cache {
    try_files $uri @rewriteapp;}

You’re done! Everything works and all assets will be rendered on the fly if it does not exist or will be directly rendered by Nginx with an expiration of one year :)


转载于:https://my.oschina.net/imot/blog/284918

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值