问题描述
我部署了一个闪亮的应用程序,在 AWS 上运行一个闪亮的服务器。 Nginx 服务器将请求重新路由到闪亮服务器的 3838 端口。
在检查 Google Page Speed Insights (https://developers.google.com/speed/pagespeed/insights/) 时,我发现我的页面上的某些图像(.webp 格式)没有被缓存,这会减慢我的页面加载速度。
我尝试在 Nginx 中设置缓存,如 here 所述,通过将以下几行添加到我的 Nginx 服务器配置中:
location ~* \.(js|webp|png|jpg|jpeg|gif)$ {
expires 365d;
add_header Cache-Control "public,no-transform";
}
然而,这会导致访问网站时不再找到我的图像。
server {
listen 80;
listen [::]:80;
# redirect all HTTP requests to HTTPS with a 301 Moved Permanently response.
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
# Reverse proxy
location / {
proxy_pass http://localhost:3838/climate-justice/;
proxy_redirect http://localhost:3838/climate-justice/ $scheme://$host/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 20d;
proxy_buffering off;
}
# OCSP stapling
ssl_stapling on;
ssl_stapling_verify on;
}
解决方法
在以下说明的帮助下解决了它:https://www.digitalocean.com/community/tutorials/how-to-implement-browser-caching-with-nginx-s-header-module-on-ubuntu-16-04
# Expires map
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
expires $expires;
. . .