Nginx proxy_cache_key 和 HEAD->GET 请求

Ale*_*der 3 nginx reverse-proxy node.js

我有以下 Nginx 配置:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

上游服务器是一个常规的 Node.js 进程。如果我激活proxy_cache_*上面的指令,HEAD请求就会被传递GET到上游服务器。如果我停用指令,HEAD请求将作为请求传递HEAD,一切都很好。

有什么建议么?

谢谢!

Bur*_*rGR 5

这个问题很老了,但仍然相关且没有答案。我只是花了几个小时寻找解决方案,Nginx 从 v .1.9.7 开始,包含一个新功能,它完全可以满足您的需求。

将其添加到您的配置中:

proxy_cache_convert_head off;
proxy_cache_methods GET HEAD;
proxy_cache_key $scheme$request_method$proxy_host$request_uri;
Run Code Online (Sandbox Code Playgroud)

第一行禁用 http 请求的转换,第二行启用除 GET 之外的 HEAD 请求的缓存。第三行将 $request_method 添加到 proxy_cache_key 中,因此头请求将被缓存为单独的文件。