
내 애플리케이션에서 외부 이미지로 핫링크하는 대신 내 웹 앱이 있는 서버에 해당 이미지를 캐시하고 싶습니다.
<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
이 시점에서 오징어 프록시는 http://dev:3128/myapp
내 PHP 앱에 문제가 없는 것처럼 작동하는 것 같습니다. 하지만 acl 라인을 주석 처리해야 하고 ALL
(그렇지 않으면 응답을 받지 못함) 요청이 <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
다음 구성을 시도해 보겠습니다. 나는 당신의 것을 정리하고(참조되지 않은 항목을 제거함) 캐시 오류가 아닌 1년 동안 모든 것을 캐시하도록 강제했습니다.
오징어 구성은 버전별로 매우 다르므로 어떤 버전을 실행하고 있습니까?
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가 웹 서버 앞에 있는 경우 를 사용하여 정규식으로 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를 사용하게 될 상황이 정확히 어떤 것인지 모르기 때문에 고전적인 Forward everything else 줄을 추가했습니다. 모든 인터넷 액세스를 프록시하지 않는 경우 해당 범위를 재정의할 수 있습니다.
답변3
오징어가 캐시처럼 작동하도록 하려면 캐싱을 허용하는 헤더를 생성하도록 PHP 스크립트를 구성해야 합니다.
오징어가 실제로 무엇이든 캐시하는 데 필요한 최소값은 다음과 같습니다.
- 날짜: (php에 의해 자동으로 생성되므로 신경쓰지 마세요)
- 최종 수정 날짜: GMT 날짜
- 캐시 제어: 여기에 입력: "public, max-age="
쉬운 방법은 "Etag:" 헤더를 발행하지 않고 "If-Modified-Since:" 요청만 처리하면 되도록 하는 것입니다(Etag:를 발행하는 경우 If-None-Match도 처리해야 합니다).
그리고 여기 있습니다.
더 깊게 알고 싶다면 읽어보세요RFC2616.
Squid와 백엔드 간의 트래픽을 효율적으로 줄이려면 스크립트에서 "If-Modified-Since"를 올바르게 구현하세요. 또한 캐싱을 방지하는 수많은 클라이언트 헤더를 삭제하거나 다시 쓸 수 있지만 이는 두 번째 단계일 뿐입니다.
답변4
기본적으로 Squid는 ?가 있는 URL을 캐시하지 않습니다. 징후. 다음을 사용해 보세요:
cache allow JPEG
또한 @Oliver S가 언급한 내용에 주의하세요. 애플리케이션이 작동하려면 캐시 가능한 상수를 제공해야 하기 때문입니다.