in Break-Fix

Nginx + Drupal + imagecache + Apache + proxy_pass + 404

Here is a workaround to the Drupal imagecache issue with Nginx running as a reverse proxy to Apache. From Drupal, ImageCache is:

ImageCache allows you to setup presets for image processing. If an ImageCache derivative doesn’t exist the web server’s rewrite rules will pass the request to Drupal which in turn hands it off to ImageCache to dynamically generate the file.

So Nginx shouldn’t try to serve static files from this directory unless they exist. Otherwise, it will generate a 404 and images will break. With this fix, we are catching the 404 generated by Nginx on static files. We are then forwarding the request to Apache (proxy_pass) so it processes it correctly. We are also caching all 404s for 1 minute to avoid slamming Apache. Be sure to “fix” all the other 404 errors so Apache does the least amount work. Most of the time, these 404 errors come from missing favicon.ico or non-manipulated images.

server {
access_log off;
error_log  logs/vhost-error_log warn;
listen    80;
server_name  www.unixy.net;
location @imagecache {
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_send_timeout   90;
proxy_read_timeout   90;
proxy_buffer_size    4k;
proxy_buffers     16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_connect_timeout 30s;
proxy_redirect  http://www.unixy.net:81   http://www.unixy.net;
proxy_pass   http://1.1.1.1:81;
proxy_set_header   Host   $host;
proxy_set_header   X-Real-IP  $remote_addr;
proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~* \.(gif|jpg|jpeg|png|wmv|avi|mpg|mpeg|mp4|js|css|mp3|swf|ico|flv)$ {

root   /home/user/public_html;

error_page    404 = @imagecache;

proxy_cache my-cache;

proxy_cache_valid  200 302  10m;

proxy_cache_valid  404  1m;

}

location / {

client_max_body_size    10m;

client_body_buffer_size 128k;

proxy_send_timeout   90;

proxy_read_timeout   90;

proxy_buffer_size    4k;

proxy_buffers     16 32k;

proxy_busy_buffers_size 64k;

proxy_temp_file_write_size 64k;

proxy_connect_timeout 30s;

proxy_redirect  http://www.unixy.net:81   http://www.unixy.net;

proxy_pass   http://1.1.1.1:81/;

proxy_set_header   Host   $host;

proxy_set_header   X-Real-IP  $remote_addr;

proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

That’s all folks. We hope this is useful to someone.

Write a Comment

Comment

Comment moderation is enabled. Your comment may take some time to appear.

Webmentions

  • case fara etaj proiecte

    It is hard to view good help ……

    I am eternally on a regular basis continuously saying that its intemperate to get actual aid, but here is….

  • Nginx + Drupal + imagecache + Apache + proxy_pass + 404 | Nginx Lighttpd Tutorial

    […] Here is a excellent tutorial show you about Nginx + Drupal + imagecache + Apache + proxy_pass + 404: Here is a workaround to the Drupal imagecache issue with Nginx running as a reverse proxy to Apache. From Drupal, ImageCache is: […]