簡體   English   中英

NGINX proxy_pass 不緩存內容

[英]NGINX proxy_pass not caching content

我在讓 NGINX 緩存我使用 proxy_pass 命令從 Dropbox 中提取的縮略圖時遇到問題。 在 NGINX 運行的同一台服務器上,我多次運行以下命令

 wget --server-response --spider  http://localhost:8181/1/thumbnails/auto/test.jpg?access_token=123

並且每次都使用 X-Cache: MISS 得到完全相同的響應

HTTP/1.1 200 OK 服務器:nginx/1.1.19 日期:2015 年 3 月 25 日星期三 20:05:36 GMT 內容類型:image/jpeg 內容長度:1691 連接:keep-alive 編譯指示:無緩存緩存控制: no-cache X-Robots-Tag: noindex, nofollow, noimageindex X-Cache: MISS

這是我的 nginx.conf 文件的內容..關於我在這里做錯了什么有什么想法嗎?

## Proxy Server Caching
proxy_cache_path  /data/nginx/cache  keys_zone=STATIC:10m max_size=1g;


## Proxy Server Setting
server {
    listen *:8181;

    proxy_cache     STATIC;
    proxy_cache_key "$request_uri";
    proxy_cache_use_stale  error timeout invalid_header updating
                   http_500 http_502 http_503 http_504;

    location ~ ^/(.*) {
    set $dropbox_api 'api-content.dropbox.com';
    set $url    '$1';

    resolver 8.8.8.8;   

    proxy_set_header    Host    $dropbox_api;

    proxy_cache     STATIC;
    proxy_cache_key     "$request_uri";
    proxy_cache_use_stale   error timeout invalid_header updating
                   http_500 http_502 http_503 http_504;

    add_header X-Cache $upstream_cache_status; 

    proxy_pass https://$dropbox_api/$url$is_args$args;
    }

    ##Error Handling
    error_page 500 502 503 504 404 /error/;  
    location = /error/ {  
    default_type text/html;
    }   
}

原來從Dropbox返回的縮略圖請求包括標題

Cache-Control: no-cache

並且Nginx將遵循這些標頭, 除非明確忽略它們,這可以通過簡單地使用將忽略任何緩存控制的以下配置行來完成。

proxy_ignore_headers    X-Accel-Expires Expires Cache-Control;

我們還遇到了將“proxy_ignore_headers”選項放在nginx.conf文件中的不同區域的問題。 最后,經過多次游戲,我們通過在“位置”塊中明確設置它來使其工作。 配置文件的完整片段可以在下面找到

    ## Proxy Server Caching
proxy_cache_path  /data/nginx/cache  levels=1:2 keys_zone=STATIC:50m inactive=2h max_size=2g;

## Proxy Server Setting
server {
    listen *:8181;

    location ~ ^/(.*) {
    set $dropbox_api 'api-content.dropbox.com';
    set $url    '$1';

    resolver 8.8.8.8;

    proxy_set_header    Host    $dropbox_api;
    proxy_hide_header   x-dropbox-thumbcachehit;
    proxy_hide_header   x-dropbox-metadata;
    proxy_hide_header   x-server-response-time;
    proxy_hide_header   x-dropbox-request-id;

    proxy_hide_header cache-control;
    proxy_hide_header expires;

    add_header cache-control "private";
    add_header x-cache $upstream_cache_status; # HIT / MISS / BYPASS / EXPIRED

    proxy_cache     STATIC;
    proxy_cache_valid       200  1d;
    proxy_cache_use_stale   error timeout invalid_header updating
                http_500 http_502 http_503 http_504;
    proxy_ignore_headers    X-Accel-Expires Expires Cache-Control;

    proxy_pass https://$dropbox_api/$url$is_args$args;
    }
}

為了緩存代理響應,Nginx和origin之間的請求應該是無cookie的:

  proxy_hide_header      Set-Cookie;
  proxy_ignore_headers   Set-Cookie;

請參閱使用失效方法的完整配置: https//gist.github.com/mikhailov/9639593

如果以上答案沒有解決您的問題,請嘗試以下操作:

proxy_cache_valid 200 2d; (或您想要的任何時間和任何響應代碼)

將此添加到您正在使用或激活您的proxy_cache <keys_zone_name>的位置。

顯然對我來說,只要我刪除proxy_cache_valid參數緩存狀態就不會出現。 文檔也沒有說這是必填字段。 讓我知道這是否適合您。 所以我們可能會更新文檔。

我希望 proxy_cache 入門頁面應該顯示您至少需要 3 個這些參數才能開始: proxy_cache_pathproxy_cacheproxy_cache_valid

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM