Ubuntu – Configure Squid to cache images explicitly called from img src=””

cachesquidUbuntu

Instead of hot-linking to external images from my application I'd like to cache them on my server where my web app resides, e.g. instead of

<img src="http://api.domain.com/image/1234.jpeg" />

I'd like to call a cache, e.g.

<img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg">

So of Squid has the image it'll pass it through, otherwise it'll retrieve and then cache it for next time. Is this possible?

I've installed squid, configured it as a reverse proxy in front of Apache. My config is below (hostname is 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

At this point the squid proxy seems to work at http://dev:3128/myapp as it serves my php app fine. But I have to comment out the ALL acl lines (otherwise I get no response) and requests for <img src="http://dev:3128/myapp/image?href=http://api.domain.com/image/1234.jpeg"> are still showing in the apache access log (whereas I'm looking for squid to cache/serve them).

http://dev:3128/myapp/image is actually a PHP script that retrieves and serves the image via fopen and fpassthru.

Best Answer

I would try the following config. I cleaned up yours (removed stuff not referenced) and forced it to cache anything for a year and not cache failures.

Note, squid configs are very version specific, so what version are you running?

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
Related Topic