【问题标题】:Setting http headers for caching with shiny server & nginx使用闪亮的服务器和 nginx 设置用于缓存的 http 标头
【发布时间】:2021-03-30 15:16:57
【问题描述】:

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

}

【问题讨论】:

    标签: nginx http-headers shiny-server cache-control


    【解决方案1】:

    在以下说明的帮助下解决了这个问题: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;
    . . .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-11
      • 2014-07-26
      • 2023-04-08
      • 2016-05-23
      • 2016-10-26
      • 2011-01-28
      • 2016-08-06
      • 2011-04-11
      相关资源
      最近更新 更多