nginx 反向代理时怎么针对文件类型来决定缓存与否? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
renothing
V2EX    NGINX

nginx 反向代理时怎么针对文件类型来决定缓存与否?

  •  
  •   renothing 2022-11-01 18:39:22 +08:00 1813 次点击
    这是一个创建于 1143 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前端 nginx 使用反向代理与我的后端一起工作,我想只缓存特定文件类型,比如图像文件, 但它似乎根本不起作用,$no_cache 总是输出默认值“proxy”,当我访问图片类文件时本应该输出“0”

    map $upstream_http_content_type $no_cache { default proxy; "~*image" 0; } proxy_store on; proxy_temp_path /tmp/ngx_cache; proxy_cache_path /tmp/ngx_cache/pcache levels=1:2 use_temp_path=on keys_zOne=pcache:10m inactive=14d max_size=3g; log_format upstreamlog '$server_addr:$host:$server_port $remote_addr:$remote_port $remote_user [$time_local] ' '"$request" $status $request_length $body_bytes_sent [$request_time] ' '"$http_referer" "$http_user_agent" "$http_x_forwarded_for" - ' 'upstream: $upstream_addr - $upstream_status - upstream_response_time: $upstream_response_time $upstream_http_content_type $skip_cache : $no_cache'; server{ ..... if ($skip_cache = false){ set $skip_cache 0; } if ($no_cache = false){ set $no_cache 0; } if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } if ($http_cache_control ~* "private") { set $skip_cache 1; } if ($upstream_http_cache_control ~* "private") { set $skip_cache 1; } location / { proxy_cache_key "$http_host$uri$is_args$args"; proxy_cache pcache; add_header X-Cache-Status $upstream_cache_status; proxy_cache_bypass $http_pragma $http_authorization $skip_cache; proxy_no_cache $http_pragma $http_authorization $skip_cache $no_cache; proxy_pass http://$backend; access_log /dev/stdout upstreamlog; } } 

    curl 测试结果

    HTTP/1.1 200 OK Server: nginx Date: Sun, 30 Oct 2022 17:22:56 GMT Content-Type: image/jpeg Content-Length: 56769 Connection: keep-alive Last-Modified: Tue, 19 Jul 2022 03:27:34 GMT ETag: "62d624a6-ddc1" Cache-Control: public, max-age=604800, s-maxage=1209600, stale-if-error=400 X-Cache-Status: MISS Accept-Ranges: bytes 

    日志里显示输出的$no_cache始终是 proxy 为什么$upstream_http_content_type没有正确匹配到“0”值呢?

    3 条回复    2022-11-02 09:12:30 +08:00
    kwh
        1
    kwh  
       2022-11-01 22:11:27 +08:00
    虽然我不懂,但是,我认为,nginx 应该不会盲目缓存吧?毕竟 nginx 又不知道你的文件什么时候会变。要是文件更改了,nginx 还在使用缓存岂不是罪过了?
    renothing
        2
    renothing  
    OP
       2022-11-01 23:56:38 +08:00
    @kwh Nginx 确实就是这样缓存的。过期需要自己 purge
    sparkssssssss
        3
    sparkssssssss  
       2022-11-02 09:12:30 +08:00   1
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3226 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 37ms UTC 11:47 PVG 19:47 LAX 03:47 JFK 06:47
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86