2

I have the LiipImagineBundle installed in my Symfony2 2.1.10 installation and I experience a little configuration problem with my nginx server. On the net I discovered this nice snippet as a great and simple nginx configuration for Symfony2 apps. I added some lines of code. Most importantly I want to be able to disable the access.log for static files like images. The following ruleset was working flawlessly:

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
}

But today I discovered that images generated by the LiipImagineBundle need to be accessed via the app.php or the app_dev.php. Otherwise they don't get generated and nginx logs the following error message:

[error] 28988#0: *733 open() "/[...].jpeg" failed (2: No such file or directory)

Which is basically a 404. The file doesn't get generated because it is not accessed via the Symfony2 application but directly.

I need a configuration that allows me to disable the access-logging and adding some caching headers to static files, but still serve them through the same route as before.

Is there a possible solution for that?

4

1 回答 1

4

问题解决了。我创建了一个匹配 bot app.php/app_dev.php等的正则表达式png|jp[e]g|gif。现在图像按请求缓存。如果未生成图像,则服务器响应201 Created和之后的响应,200 Ok或者304 Not Modified取决于它之前是否被缓存。这是我相应的 nginx 配置:

location ~ ^/(app_dev|app)\.php(.*?)\.(?:ico|gif|jpe?g|png)$ {
    expires 18h;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;

    # still redirect because we are using LiipImagineBundle!
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}

# more static files:
location ~* \.(?:css|js)$ {
    expires 18h;
    add_header Cache-Control "publi, must-revalidate, proxy-revalidate";
    access_log off;
}

location ~ ^/(app_dev|app)\.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS off;
}

我希望这有帮助。

于 2013-06-05T10:10:56.867 回答