정적 파일 캐싱을 위한 Nginx 구성

정적 파일 캐싱을 위한 Nginx 구성

AngularJS 애플리케이션을 올바르게 제공하도록 Nginx를 구성하는 데 몇 가지 문제가 있습니다. 서버는 다음과 같이 구성됩니다.

www.example.com we have the landing page of the application
www.example.com/app we have the application itself

응용 프로그램의 경로는 다음과 같습니다.

/usr/share/nginx/html/example.com/app/

정적 파일은 다음 안에 있습니다.

/usr/share/nginx/html/example.com/app/public/app/assets

이제 랜딩 페이지와 애플리케이션의 모든 html 파일에 대해 캐싱을 "no-cache"로 설정하고 모든 js, css 및 이미지 파일에 대한 캐시를 60일로 설정하고 싶습니다.

이것은 현재 nginx 서버 구성입니다.

server {
listen 80;

index index.html;

server_name example.com www.example.com;

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
    expires 60d;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

location ^~ /app {
    alias /usr/share/nginx/html/example.com/app/public/;
    expires -1;
    add_header Pragma "no-cache";
}

location / {
    root /usr/share/nginx/html/example.com;
    expires -1;
    add_header Pragma "no-cache";
    add_header Cache-Control "no-store, no-cache, must-revalidate,  post-check=0, pre-check=0";
  }
}

이제 문제는 위치 지시문이 다음과 같다는 것입니다.

location ~* \.(?:ico|css|js|gif|jpe?g|png)$

절대 실행되지 않으므로 캐시는 /app 지시문에 정의된 대로 no-cache로 설정됩니다.

어떤 아이디어?

감사해요

답변1

nginx 문서의 인용문:

가장 긴 일치 접두사 위치에 "^~" 수정자가 있는 경우 정규식은 확인되지 않습니다.

따라서 여기서 문제는 귀하의 location ^~ /app정의입니다. 수정 ^자는 nginx가 이미지의 정규식을 무시하도록 만듭니다.

대신 사용해야 합니다 location /app. 이 지점에서는 정규식 일치가 필요하지 않으며 간단한 접두사 일치만으로 충분합니다.

답변2

문제는 "~*"인 것 같습니다. "~"로 시도해 보세요. 예:

location ~ \.(?:ico|css|js|gif|jpe?g|png)$

관련 정보