2015-11-13 28 views
1

我試圖讓我的頭緩存圖像爲我的開放源代碼圖像託管serivce PictShare在nginx反向代理的所有文件夾級別緩存圖像

Pictshare有一個智能查詢系統,上傳的圖像可以在「虛擬子目錄」中更改圖像。例如,這是鏈接到上傳的計算器標誌:https://www.pictshare.net/300/6cb55fe938.png

因爲我處理了很多流量:https://www.pictshare.net/6cb55fe938.png

我可以通過圖像名稱前添加/300/到URL調整到300寬最近我希望我的nginx代理能夠從所有虛擬子文件夾中緩存所有圖像,但它不起作用。我讀過很多文章和很多stackoverflow的帖子,但沒有解決方案爲我工作。

到目前爲止,這是我的虛擬主機生產文件

proxy_cache_path /etc/nginx/cache/pictshare levels=1:2 keys_zone=pictshare:50m max_size=1000m inactive=30d; 
proxy_temp_path /etc/nginx/tmp 1 2; 
proxy_cache_key "$scheme$request_method$host$request_uri"; 

proxy_ignore_headers "Set-Cookie"; 
proxy_hide_header "Set-Cookie"; 
proxy_buffering on; 

server { 
... 
location/{ 
     proxy_pass   http://otherserver/pictshare/; 
     include /etc/nginx/proxy_params; 

     location ~* \.(?:jpg|jpeg|gif|png|ico)$ { 
      expires max; 
      proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; 
      proxy_cache_valid 200 301 302 1y; 
      proxy_cache pictshare; 
      proxy_pass   http://otherserver/pictshare$request_uri; 
     } 

    } 
} 

的問題是,沒有文件緩存,我看到了代理目的地每個圖像的要求。

我就開始工作的唯一途徑是通過增加一個特殊的位置到具有緩存明確啓用主機文件:

location /cached { 
     proxy_cache_valid 200 1y; 
     proxy_cache pictshare; 
     expires 1y; 

     proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; 

     include /etc/nginx/proxy_params; 

     proxy_pass   http://otherserver/pictshare/thumbs/; 
     proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; 

     } 

這個解決方案的明顯的問題是,圖像僅在緩存要求與/cached如開始:https://www.pictshare.net/cached/6cb55fe938.png

添加緩存命令到根目錄下沒有選擇我,因爲我不想在形式和頁面進行高速緩存,只是圖像

哪裏是我的錯誤?

回答

2
proxy_cache_path /etc/nginx/cache/pictshare levels=1:2 keys_zone=my_cache:50m max_size=3g inactive=180m; 
proxy_temp_path /etc/nginx/tmp 1 2; 
proxy_cache_key "$scheme$request_method$host$request_uri"; 

location ~* ^.+\.(jpe?g|gif|png|ico|pdf)$ { 
    access_log off; 
    include /etc/nginx/proxy.conf; 
    proxy_pass http://backend; 
    proxy_cache pictshare; 
    proxy_cache_valid any 12h; 
    add_header X-Proxy-Cache $upstream_cache_status; 
    root /var/www/public_html/cached; } 

location/{ 
    include /etc/nginx/proxy.conf; 
    proxy_pass http://backend; 
    root /var/www/public_html; 
} 

nginx首先搜索由文字字符串給出的最具體的前綴位置,而不管列出的順序如何。在上面的配置中,唯一的前綴位置是「/」,並且因爲它匹配任何請求,所以它將被用作最後的手段。然後nginx按照配置文件中列出的順序檢查正則表達式給出的位置。第一個匹配表達式會停止搜索,nginx將使用此位置。如果沒有正則表達式匹配請求,那麼nginx使用前面找到的最具體的前綴位置。 http://nginx.org/en/docs/http/request_processing.html

+0

我們是人,不會是你寫的兩行解釋什麼是錯的OP的代碼和爲什麼你應該更好地工作? – phaberest

+0

評論...是不是沒人花時間:) –

+0

你快樂嗎? :D – Yura