我是 nginx 缓存方面的新手。我一直试图在服务器上设置缓存,但即使在添加了 css/js/和图像配置后,X-Cache-Status 仍显示 MISS。这导致加载时间过长,即使在流量较低的情况下速度也很慢。知道我遗漏了什么吗?
以下是响应标头:
Cache-Control
max-age=86400
Connection
keep-alive
Content-Encoding
gzip
Content-Type
text/css
Date
Wed, 18 Jan 2017 16:00:34 GMT
Expires
Thu, 19 Jan 2017 16:00:34 GMT
Last-Modified
Mon, 19 Oct 2015 09:26:18 GMT
Server
nginx/1.4.6 (Ubuntu)
Transfer-Encoding
chunked
Vary
Accept-Encoding
X-Cache-Status
MISS
我正在使用 https,将 http 重定向到 https。这是我设置的启用站点的文件。
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=TOMCAT:50m max_size=100m;
server {
listen 80;
server_name *.xyz.com;
default_type text/html;
return 307 https://$host$request_uri;
root /var/lib/tomcat7/webapps;
index index.html index.jsp;
location / {
set $no_cache "";
if ($request_method !~ ^(GET|HEAD)$) {
set $no_cache "1";
}
if ($no_cache = "1") {
add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
add_header X-Microcachable "0";
}
if ($http_cookie ~* "_mcnc") {
set $no_cache "1";
}
if ($request_uri ~* ".(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
expires 1d;
access_log off;
break;
}
proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;
proxy_cache TOMCAT;
proxy_cache_key $scheme$host$request_method$request_uri;
proxy_cache_valid 200 302 1s;
proxy_cache_valid 301 1s;
proxy_cache_valid any 1s;
proxy_cache_use_stale updating;
sendfile off;
proxy_pass http://127.0.0.1:8080/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_max_temp_file_size 0;
add_header X-Cache-Status $upstream_cache_status;
#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
server {
server_name *.xyz.com;
listen 443;
ssl on;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#make sure you already have this certificate pair!
ssl_certificate /etc/ssl/certs/xyz.crt;
ssl_certificate_key /etc/ssl/certs/xyz.key;
ssl_session_cache shared:SSL:10m;
location / {
set $no_cache "";
if ($request_method !~ ^(GET|HEAD)$) {
set $no_cache "1";
}
if ($no_cache = "1") {
add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
add_header X-Microcachable "0";
}
if ($http_cookie ~* "_mcnc") {
set $no_cache "1";
}
if ($request_uri ~* ".(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
expires 1d;
access_log off;
break;
}
proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;
proxy_cache TOMCAT;
proxy_cache_key $scheme$host$request_method$request_uri;
proxy_cache_valid 200 302 1s;
proxy_cache_valid 301 1s;
proxy_cache_valid any 1s;
proxy_cache_use_stale updating;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
add_header X-Cache-Status $upstream_cache_status;
proxy_redirect off;
proxy_connect_timeout 240;
proxy_send_timeout 240;
proxy_read_timeout 240;
# note, there is not SSL here! plain HTTP is used
proxy_pass http://127.0.0.1:8081;
}
}
有什么建议 ?
答案1
只需添加此内容。这会将过期日期设置为最大日期(我记得是 2037 年),并将缓存控制设置为 10 年。
location ~* \.(css|gif|ico|jpeg|jpg|js|png|woff|woff2|ttf|ttc|otf|eot)$ {
expires max;
log_not_found off;
}