4

I have the following Nginx config:

http {
    ...    
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=one:8m max_size=3000m inactive=600m;
    proxy_temp_path /var/tmp;
    ...

    upstream webhook_staging {
        server 127.0.0.1:4001;
        keepalive 64;
    }    

    location /webhooks/incoming_mails {
        client_max_body_size 60m;
        proxy_set_header     X-Real-IP $remote_addr;
        proxy_set_header     X-Forwarded-For $remote_addr;
        proxy_set_header     X-Forwarded-Proto $scheme;
        proxy_set_header     Host $http_host;
        proxy_set_header     Connection "";
        proxy_http_version   1.1;

        # Does not work for HEAD requests
        #>> proxy_cache one;
        #>> proxy_cache_key      $scheme$host$request_uri;

        proxy_pass           http://webhook_staging;
    }
}

The upstream server is a regular Node.js process. If I activate the proxy_cache_* directives above, a HEAD request is passed a GET request to the upstream server. If I deactivate the directives the HEAD request is passed as a HEAD request and everything is fine.

Any suggestions?

Thanks!

2 Answers 2

6

This question is very old, but it is still relevant and unanswered. I just spend several hours finding a solution to this, Nginx from v .1.9.7, include a new feature which do exactly what you want.

Add this to your config:

proxy_cache_convert_head off;
proxy_cache_methods GET HEAD;
proxy_cache_key $scheme$request_method$proxy_host$request_uri;

The first line disables conversion of the http request, and the second line enables caching of the HEAD requests in addition to GET. Third line add $request_method to the proxy_cache_key, so head requests will be cached as a separate file.

0

To disable caching on HEAD requests you have to use some extra logic, e.g.:

server {
  ...

  resolver 127.0.0.1;

  location / {

    error_page 420 = @skip_cache;
    error_page 421 = @use_cache;

    if ( $request_method = 'GET' )
    {
      return 421;
    }

    return 420;
  }

  location @use_cache {
    internal;

    proxy_cache cache;
    proxy_cache_key "...";
    proxy_cache_valid 200 1h;
    proxy_buffering off;
    proxy_pass ...;
  }

  location @skip_cache {
    internal;
    proxy_buffering off;
    proxy_pass ...;
  }
}

I haven't found a way to prevent nginx from changing HEAD to GET when the cache is enabled :-(.

1
  • There isn't a way, the cache only supports GETs, still in 1.7.
    – mikaraento
    Oct 12, 2014 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .