而不是从我的应用程序热链接到外部图像,我想将它们caching在我的Web应用程序所在的服务器上,例如,而不是
<img src="http://api.domain.com/image/1234.jpeg" />
我想调用caching,例如
<img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg">
所以Squid有它将通过它的形象,否则它将检索,然后caching下一次。 这可能吗?
我已经安装了鱿鱼,将其configuration为Apache前面的反向代理。 我的configuration在下面(主机名是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
在这一点上,鱿鱼代理似乎在http://dev:3128/myapp因为它服务我的PHP应用程序罚款。 但是我必须注释掉ALL acl行(否则我没有得到任何回应)并请求<img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg">仍然显示在Apache访问日志(而我正在寻找鱿鱼来caching/服务他们)。
http://dev:3128/myapp/image实际上是一个PHP脚本,通过fopen和fpassthru检索并提供图像。
我会尝试下面的configuration。 我清理你的(删除的东西没有引用),并强迫它caching一年的任何东西,而不是caching失败。
请注意,squid的configuration是非常特定的版本,所以你运行的是什么版本?
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
如果squid位于Web服务器的前面,可以使用正则expression式设置一个ACL(虽然这需要您为所有外部域设置正则expression式),但是urlpath_regex可以工作,但是它不包括主机名..如果文件的path与服务器上托pipe的path相同,这可能是重要的。
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到底是什么情况,所以我添加了经典的前向一切线,如果你不代理所有的互联网访问,你可能想重新定义范围。
如果你想鱿鱼行为像一个caching,你需要configuration你的PHP脚本生成头允许caching。
鱿鱼实际caching的最低限度是:
最简单的方法就是不发出“Etag:”头,这样你只需要处理“If-Modified-Since:”请求(如果你发出Etag:你将不得不处理If-None-Match)。
在这里,你走了。
如果你想深入,你应该阅读rfc 2616 。
如果你想有效地减less鱿鱼和你的后端之间的stream量,在你的脚本中正确实施“If-Modified-Since”。 你也可以删除/重写许多客户端头,这将防止caching,但这只是第二步。
如果使用“外部”图像,则表示图像不在您自己的服务器上(因此不在您的服务器后面),您需要自己做一些欺骗,代理和caching图像。 所以让我们设置一个例子来确保我正确理解你:
如果图像只在api.example.com上,这很容易,甚至不需要鱿鱼。 你可以用mod_proxy和mod_cache来做到这一点。 使<img ..>标签看起来像<img src="http://www.example.org/images/api/path/to/image/here" />然后在Apache中使用ProxyPass传递/images/api代理到api.example.com和mod_cacheconfiguration来caching它。
如果你真的想使用鱿鱼,你需要使鱿鱼更聪明。 我仍然会使<img ..>标签与上面相同,并使用ICAP帮助程序来重写/ images / api /的请求以转到api.example.com。 Squid可以请求和caching这些。
默认情况下,Squid不caching与? 标志。 试试:
cache allow JPEG
还要注意@Oliver S提到的,因为你的应用程序应该服务于可caching的工作。