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?

有帮助吗?

解决方案

Problem solved. I've created a regex that matches bot app.php/app_dev.php and png|jp[e]g|gif etc. Now images are cached as requested. If an Image was not generated the server responses with 201 Created and afterwards with either 200 Ok or 304 Not Modified depending on wether it was cached before or not. Here is my corresponding nginx config:

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;
}

I hope this helps.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top