使用闪亮的服务器和 nginx

时间:2020-12-20 17:48:50

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

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

}

1 个答案:

答案 0 :(得分:0)

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