캐시 제어를 위해 HTTP 헤더를 설정하는 방법은 무엇입니까?
내 사이트에 브라우저 캐싱을 활성화하는 방법은 무엇입니까? 캐시 헤더를 다음과 같이 어딘가에 넣습니까?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
Cache-Control:public;
>
최신 버전의 XAMPP에서 개발중인 최신 버전의 PHP를 사용하고 있습니다.
HTML에 캐시 컨트롤을 사용하려면, 당신이 사용하는 메타 태그를 , 예를 들어,
<meta http-equiv="Cache-control" content="public">
컨텐츠 필드의 값은 아래 4 가지 값 중 하나로 정의됩니다.
Cache-Control헤더 에 대한 일부 정보 는 다음과 같습니다.
HTTP 1.1. 허용되는 값 = PUBLIC | 개인 정보 | 노 캐시 | 상점 없음.
공개-공개 공유 캐시에 캐시 될 수 있습니다.
개인-개인 캐시에만 캐시 될 수 있습니다.
캐시 없음-캐시되지 않을 수 있습니다.
No-Store-캐시 될 수 있지만 아카이브되지 않을 수 있습니다.지시문 CACHE-CONTROL : NO-CACHE는 캐시 된 정보를 사용하지 말고 대신 요청을 원본 서버로 전달해야 함을 나타냅니다. 이 지시문은 PRAGMA : NO-CACHE와 동일한 의미를 갖습니다.
캐시없는 요청이 HTTP / 1.1 호환이 아닌 서버로 전송 될 때 클라이언트 는 PRAGMA : NO-CACHE 및 CACHE-CONTROL : NO-CACHE를 모두 포함해야합니다. EXPIRES도 참조하십시오.
참고 : 브라우저보다 많은 영향을 줄 수 있지만 정보를 캐시 할 수있는 프록시 및 기타 중개자에서는 META 문보다 HTTP에서 캐시 명령을 지정하는 것이 좋습니다.
다음 을 사용하여 PHP에서 헤더를 설정할 수 있습니다 .
<?php
//set headers to NOT cache a page
header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
header("Pragma: no-cache"); //HTTP 1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
//or, if you DO want a file to cache, use:
header("Cache-Control: max-age=2592000"); //30days (60sec * 60min * 24hours * 30days)
?>
사용되는 정확한 헤더는 요구 사항에 따라 다릅니다 (및 HTTP 1.0 및 / 또는 HTTP 1.1 을 지원해야하는 경우 ).
내가 쓴 것처럼 파일을 사용하는 것이 가장 좋습니다 .htaccess. 그러나 캐시에 내용을 남길 때주의하십시오.
사용하다:
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
위치 : 604800 = 7 일
PS : 헤더를 재설정하는 데 사용할 수 있습니다
http://www.askapache.com/htaccess/apache-speed-cache-control.html 의 페이지는 다음과 같은 사용을 제안합니다.
캐시 제어 헤더 추가
이것은 루트 .htaccess 파일에 있지만 httpd.conf에 액세스 할 수 있으면 좋습니다.
이 코드는 FilesMatch 지시문과 Header 지시문을 사용하여 특정 파일에 캐시 제어 헤더를 추가합니다.# 480 weeks <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$"> Header set Cache-Control "max-age=290304000, public" </FilesMatch>
이것은 .htaccess내 실제 웹 사이트에서 사용한 최고입니다 .
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
##Tweaks##
Header set X-Frame-Options SAMEORIGIN
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_headers.c>
Header set Connection keep-alive
<filesmatch "\.(ico|flv|gif|swf|eot|woff|otf|ttf|svg)$">
Header set Cache-Control "max-age=2592000, public"
</filesmatch>
<filesmatch "\.(jpg|jpeg|png)$">
Header set Cache-Control "max-age=1209600, public"
</filesmatch>
# css and js should use private for proxy caching https://developers.google.com/speed/docs/best-practices/caching#LeverageProxyCaching
<filesmatch "\.(css)$">
Header set Cache-Control "max-age=31536000, private"
</filesmatch>
<filesmatch "\.(js)$">
Header set Cache-Control "max-age=1209600, private"
</filesmatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</filesMatch>
</IfModule>
Apache 서버의 경우 만료 및 캐시 제어 헤더 설정을 위해 mod_expires 를 확인해야합니다 .
또는 Header 지시문을 사용 하여 직접 Cache-Control을 추가 할 수 있습니다 .
Header set Cache-Control "max-age=290304000, public"
메타 캐시 제어 태그를 사용하면 웹 게시자는 캐시에서 페이지를 처리하는 방법을 정의 할 수 있습니다. 캐시 가능한 것, 캐시에 의해 저장 될 수있는 것, 만료 메커니즘의 수정, 재확인 및 재로드 제어를 선언하는 지시문이 포함됩니다.
허용되는 값은 다음과 같습니다.
공개-공개 공유 캐시에 캐시
될 수 있음 비공개-개인 캐시에만
캐시 될 수 있음 캐시
없음- 캐시 되지 않음-저장소 없음-캐시 될 수 있지만 아카이브되지 않음
대소 문자 구분에주의하십시오. 웹 페이지 소스에 다음 메타 태그를 추가하십시오. 태그 끝의 철자 차이는 "/> = xml 또는"> = html을 사용한다는 것입니다.
<meta http-equiv="Cache-control" content="public">
<meta http-equiv="Cache-control" content="private">
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Cache-control" content="no-store">
소스-> 메타 태그
OWASP는 다음을 권장합니다.
가능할 때마다 캐시 제어 HTTP 헤더가 캐시 없음, 저장소 없음, 재확인, 개인용으로 설정되어 있는지 확인하십시오. pragma HTTP 헤더는 no-cache로 설정되어 있습니다.
<IfModule mod_headers.c>
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</IfModule>
참고 URL : https://stackoverflow.com/questions/4480304/how-to-set-http-headers-for-cache-control
'development' 카테고리의 다른 글
| OS 수준의 시스템 정보를 얻습니다 (0) | 2020.04.16 |
|---|---|
| uintptr_t 데이터 형이란 무엇입니까 (0) | 2020.04.16 |
| cout << 연산자를 사용할 때 선행 0으로 int를 채우려면 어떻게해야합니까? (0) | 2020.04.16 |
| switch 문에 항상 기본 절이 포함되어야합니까? (0) | 2020.04.16 |
| Ansible 호스트에서 명령 실행 (0) | 2020.04.16 |