2013-10-22 3 views
13

내 정적 파일을 캐시하는 규칙을 정의하는 데 문제가 있습니다. 나는이 해결책을 찾았습니다 :NGINX 캐시 정적 파일

location ~* \.(ico|js|css|png|gif|jpe?g)$ { 
    expires 7d; 
} 

실제로 내가 필요한 것 같습니다. 문제는이 코드를 NGINX.conf에 포함하면 정적 파일이 더 이상 제공되지 않으며 내 사이트가 비어 있다는 것입니다. 이 결과를 초래할 수있는 아이디어/힌트는 무엇입니까? 어쩌면 정적 파일이 다른 디렉토리에 배포된다는 것을 추가해야 할 수도 있습니다. 내 NGINX 설정 파일은 다음과 같습니다 :

server { 
    server_name    bla.domain.com; 

    listen     80; 
    root      /var/repo/; 

    location/{ 
    default_type   text/html; 
    index     index.html; 

    if ($request_method !~ ^(GET)$) { 
     return 444; 
    } 

    if ($http_user_agent ~* LWP::Simple|BBBike|wget) { 
     return 403; 
    } 

    if ($http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen)) { 
     return 403; 
    } 
    } 

    location /bf/football/ { 
    alias /var/repos/f20; 
    } 

    location /bf/f20/ { 
    alias /var/repo/f20; 
    } 

    location /bf/zoo/ { 
    alias /var/repo/zoo/; 
    } 

    location /kbloader/ { 
    alias /var/repo/kbloader/; 
    } 
} 

사람이 나를 도와 주거나 올바른 방향으로 나를 가리킬 수 있다면 좋을 것입니다.

건배, Szop 다른 위치 이전

+0

당신이 전체 구성을 게시시겠습니까? – alfredocambera

답변

28

넣어이 :

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { 
     expires 30d; 
     add_header Vary Accept-Encoding; 
       access_log off; 
    } 

작동합니다.

당신은이를 사용할 수 있습니다

## All static files will be served directly. 
    location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|svg)$ { 

     access_log off; 
     expires 30d; 
     ## No need to bleed constant updates. Send the all shebang in one 
     ## fell swoop. 
     tcp_nodelay off; 
     ## Set the OS file cache. 
     open_file_cache max=3000 inactive=120s; 
     open_file_cache_valid 45s; 
     open_file_cache_min_uses 2; 
     open_file_cache_errors off; 
    } 
+1

이 인스턴스에서 캐시를 지우려면 어떻게해야합니까? nginx를 다시 시작하는 것만 큼 간단할까요? – hamstar

+2

이 예제는 서버 측 캐시가 아니며 브라우저 캐시를 관리합니다. 서버에서 정적 파일을 업데이트하는 경우 shift + F5/ctrl + F5 –

+0

으로 다시로드하여 브라우저 캐시를 지울 수 있습니다. 클라이언트에서 새 파일을 즉시 업데이트하거나 만료 될 때까지 기다려야합니까? – nXqd