使用闪亮的服务器和 nginx

问题描述

我部署了一个闪亮的应用程序,在 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"; 
}

然而,这会导致访问网站时不再找到我的图像。

  1. 我必须在 Nginx 中启用缓存而不是在闪亮的服务器中的某处启用缓存是否正确?
  2. 如果是,上面的解决方案有什么问题?

这里是Nginx的conf文件,没有任何添加

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;
. . .

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...