
Nginx를 사용하여 일부 정적 파일을 캐시하고 싶습니다. 하지만 어떻게 든 작동하게 할 수 없습니다.
이것은 나의 nginx.conf
:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
#multi_accept on;
}
http {
#GZIP
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types application/javascript application/json application/ld+json application/xml font/eot font/otf font/ttf text/css text/javascript text/plain text/xml;
# SERVERS
server {
listen 80;
server_name example.com;
if ($http_host ~* ^www\.(.*)$ )
{
return 301 https://$1$request_uri;
}
return 301 https://$http_host$request_uri;
}
server {
listen 443 ssl;
if ($http_host ~* ^www\.(.*)$ )
{
return 301 $scheme://$1$request_uri;
}
#SSL
ssl_certificate /root/.acme.sh/example.com/fullchain.cer;
ssl_certificate_key /root/.acme.sh/example.com/example.com.key;
server_name example.com;
# Pass all traffic to my webapplication
location / {
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
}
#Browser caching
location ~* \.(js|css)$ {
expires 180d;
add_header Pragma "public";
add_header Cache-Control "public";
}
location ~* \.(jpg|jpeg|png|webp|woff|woff2|ttf)$ {
expires 365d;
add_header Pragma "public";
add_header Cache-Control "public";
}
}
}
문제는 "브라우저 캐싱" 부분에 있습니다. 이 코드 블록을 활성화하면 내 사이트가 로드되지만 모든 CSS 파일, 자바스크립트 파일 및 이미지는 404를 반환합니다. 해당 파일이 내 location /
.
복사/붙여넣기로 문제를 해결할 수 있었습니다.
proxy_set_header Host $host;
proxy_pass http://localhost:8080;
내 모든 location
블록에 포함되어 있지만 실제로는 우아하지 않고 실제로 내 사이트가 훨씬 느린 느낌을 받았습니다...
location
또한 블록의 브라우저 캐싱을 위해 두 블록을 이동하여 location \
후자가 '부모' 역할을 하도록 시도했습니다. 그러나 그것은 404를 반환하는 이미지 등의 동작에 영향을 미치지 않았습니다.
Nginx에서 정적 파일 캐싱을 어떻게 구성합니까?
편집하다:http
내 -block 에 다음을 추가했습니다 .
map $uri $cache_control {
~/Website/assets/media/images "public, no-transform";
}
map $uri $expire {
~/Website/assets/media/images 365d;
}
server
my -block 에 다음을 추가했습니다 .
expires $expire;
add_header Cache-Control $cache_control;
아무것도 캐시되지 않습니다.
답변1
@TeroKilkanen이 제안하는 것처럼 nginx를 통해 파일 시스템에서 직접 정적 자산을 제공할 수 없는 경우 다음과 유사한 기술을 사용할 수 있습니다.이것답변:
map $uri $expire {
~\.(?:j|cs)s$ 180d;
~\.(?:jpe?g|png|webp|woff2?|ttf)$ 365d;
default off;
}
map $uri $cache_control {
~\.(?:js|css|jpe?g|png|webp|woff2?|ttf)$ public;
}
server {
...
expires $expire;
add_header Pragma $cache_control;
add_header Cache-Control $cache_control;
...
}
요청 URI가 정규식과 일치하지 않으면 $cache_control
변수는 빈 값을 가지며 nginx는 응답에 헤더를 Pragma
추가 하지 않습니다.Cache-Control
답변2
이는 실제로 귀하의 질문에 대한 답변은 아니지만 nginx로 정적 자산을 제공하는 선호되는 방법을 보여줍니다.
동일한 호스트에서 웹 애플리케이션을 실행하는 것으로 보이므로 nginx를 사용하여 정적 파일을 직접 제공하는 것이 좋습니다.
root /path/to/webroot;
location ~* \.(js|jss)$ {
expires 180d;
add_header Pragma "public";
add_header Cache-Control "public;
try_files $uri =404;
}
location ~* \.(jpg|jpeg|png|webp|woff|woff2|ttf)$ {
expires 365d;
add_header Pragma "public";
add_header Cache-Control "public";
try_files $uri =404;
}
여기에는 여전히 캐싱에 대한 중복 정의가 있습니다. 별도의 파일에 지시문을 지정하고 include
구성에 파일을 포함시키는 데 사용하면 일부 중복을 제거할 수 있습니다 .
제출 하려면 다음을 입력합니다 proxy_header.conf
.
add_header Pragma "public";
add_header Cache-Control "public";
그리고 귀하의 구성에서 :
location ~* ... {
include /path/to/webroot;
expires 365d;
}