配置 Squid 来缓存从 img src="" 明确调用的图像

配置 Squid 来缓存从 img src="" 明确调用的图像

我不想从我的应用程序热链接到外部图像,而是想将它们缓存在我的 Web 应用程序所在的服务器上,例如

<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 有图像,它会将其传递过去,否则它会检索并缓存以供下次使用。这可能吗?

我已经安装了 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 应用程序提供服务。但是我必须注释掉ALLacl 行(否则我将得不到任何响应),并且 的请求<img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg">仍然显示在 apache 访问日志中(而我正在寻找 squid 来缓存/为它们提供服务)。

http://dev:3128/myapp/imagefopen实际上是一个 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_regexurlpath_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 脚本来生成允许缓存的标头。

squid 实际缓存内容的最低要求是:

  • 日期:(由 php 自动生成,所以不必担心)
  • 最后修改时间:GMT 日期
  • Cache-Control:在此处输入:“public,max-age=”

简单的方法是不发出“Etag:”标头,这样您只需处理“If-Modified-Since:”请求(如果您发出 Etag:您还必须处理 If-None-Match)。

好了,就这样。

如果你想深入了解,你应该阅读rfc2616

如果您想有效减少 squid 和后端之间的流量,请在脚本中正确实现“If-Modified-Since”。您还可以删除/重写大量可防止缓存的客户端标头,但这只是第二步。

答案4

默认情况下,Squid 不会缓存带有 ? 符号的 URL。请尝试:

cache allow JPEG

还要注意@Oliver S 提到的内容,因为您的应用程序应该将内容作为可缓存的内容来工作。

相关内容