大网数据广告活动
服务器运维问答

Nginx proxy_cache_key and HEAD->GET request

· 52CE 我爱测

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 6…

问题原文

原帖链接 · Alexander · CC BY-SA 3.0

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!

社区高分解答

原帖链接 · BurnerGR · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-08-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。