
我不想從我的應用程式熱連結到外部圖像,而是想將它們緩存在我的網路應用程式所在的伺服器上,例如,而不是
<img src="http://api.domain.com/image/1234.jpeg" />
我想調用緩存,例如
<img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg">
因此,Squid 擁有圖像,它將傳遞它,否則它將檢索並緩存它以供下次使用。這可能嗎?
我已經安裝了魷魚,並將其配置為 Apache 前面的反向代理。我的配置如下(主機名稱是 dev):
acl manager proto cache_object
acl localhost src 127.0.0.1/32 ::1
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
acl dev_users dstdomain dev
http_access allow dev_users
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
acl JPEG url_regex -i \.myapp/image?href=http://api.domain.com/image/*.jpeg$
#acl ALL dst 0.0.0.0/0.0.0.0
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost
http_access deny all
http_port 3128 accel defaultsite=dev vhost
cache_peer 127.0.0.1 parent 80 0 no-query originserver name=dev
cache_peer_access dev allow dev_users
cache_peer_access dev deny all
cache_dir ufs /var/spool/squid3 100 16 256
coredump_dir /var/spool/squid3
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880
refresh_pattern . 0 20% 4320
never_direct allow JPEG
#always_direct allow ALL
此時,squid 代理似乎可以正常工作,http://dev:3128/myapp
因為它可以很好地為我的 php 應用程式提供服務。但我必須註解掉ALL
acl 行(否則我不會得到回應),並且<img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg">
apache 存取日誌中仍然顯示請求(而我正在尋找魷魚來快取/服務它們)。
http://dev:3128/myapp/image
fopen
實際上是一個 PHP 腳本,透過和檢索並提供圖像fpassthru
。
答案1
我會嘗試以下配置。我清理了你的(刪除了未引用的內容)並強制它緩存任何內容一年而不是快取失敗。
請注意,squid 配置非常特定於版本,那麼您執行的是哪個版本?
cache_dir ufs /var/spool/squid3 100 16 256
coredump_dir /var/spool/squid3
acl localhost src 127.0.0.1 ::1
acl PURGE method PURGE
http_access allow PURGE localhost
acl manager proto cache_object
http_access allow manager localhost
acl dev_users dstdomain dev
http_access allow dev_users
http_port 3128 accel defaultsite=dev vhost
cache_peer 127.0.0.1 parent 80 0 no-query originserver name=dev
cache_peer_access dev allow dev_users
cache_peer_access dev deny all
# Don't cache 404's
negative_ttl 0
# Cache everything for a year
refresh_pattern . 1440 100% 525949 override-expire ignore-reload ignore-no-cache ignore-no-store ignore-must-revalidate ignore-private ignore-auth
#cache JPEG but not anything else
acl JPEG url_regex -i .*image\?href=http.*api.discogs.com.*image.*\.jpeg$
acl to_localhost dst 127.0.0.0/8
cache allow JPEG
cache deny to_localhost
答案2
如果squid位於Web伺服器前面,您可以使用正規表示式設定ACL(儘管這需要您為所有外部網域設定正規表示式),或者使用url_regex
,urlpath_regex
可以工作,但它排除主機名稱。的文件的路徑相同,這可能很重要。
acl JPGS url_regex -i \.somedomain.com/images/*.jpg$ #Matches JPGs from somedomain
acl GIFS url_regex -i \.somedomain.com/images/*.gif$ #Matches GIFs from somedomain
acl ALL dst 0.0.0.0/0.0.0.0 #Matches Everything else
never_direct allow JPGS
never_direct allow GIFS
always_direct allow ALL
我不知道您將在這裡使用 Squid 的具體情況,因此我添加了經典的轉發所有其他行,如果您不代理所有互聯網訪問,您可能需要重新定義該範圍。
答案3
如果你想讓squid像快取一樣運行,你需要配置你的php腳本來產生允許快取的標頭。
魷魚實際緩存任何內容的最低限度是:
- 日期:(由php自動生成,所以不用費心)
- 最後修改時間:gmt 日期
- 快取控制:放在這裡:“public,max-age =”
簡單的方法是不發出“Etag:”標頭,這樣您只需處理“If-Modified-Since:”請求(如果您發出 Etag:您還必須處理 If-None-Match )。
就這樣吧。
如果你想更深入,你應該閱讀RFC 2616。
如果您想有效減少魷魚和後端之間的流量,請在腳本中正確實現「If-Modified-Since」。您還可以刪除/重寫大量客戶端標頭,這將阻止緩存,但這只是第二步。
答案4
預設情況下,Squid 不會緩存帶有 ? 的 URL。符號。嘗試使用:
cache allow JPEG
另請注意@Oliver S 提到的內容,因為您的應用程式應該將內容提供為可快取才能運作。