nginx - 如何使用 NGINX 缓存 NextJS 10.0 图像

标签 nginx next.js nextjs-image

我们想使用 NGINX 启动 NextJS 10 应用程序,因此我们使用类似于以下的配置:

location /_next/static/ {
    alias /home/ec2-user/my-app/.next/static/;
    expires 1y;
    access_log on;
}

它工作得很好,它缓存了我们的静态数据一年,但是当我们使用 NextJS images 时我无法在动态调整大小的图像上添加 expires 标签。

如果我这样做:

location /_next/image/ {
    alias /home/ec2-user/my-app/.next/image;
    expires 1y;
    access_log on;
}

它只返回图像上的 404。

这是我的服务器部分 NGINX 配置:

server {
    listen 80;
    server_name *.my-website.com;
    # root        /usr/share/nginx/html;
    # root /home/ec2-user/my-app;
    charset     utf-8;

    client_max_body_size    20M;
    client_body_buffer_size 20M;

    proxy_connect_timeout    600;
    proxy_send_timeout       600;
    proxy_read_timeout       600;
    send_timeout             600;

    underscores_in_headers on;

    add_header X-Frame-Options SAMEORIGIN always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Referrer-Policy "same-origin" always;

    location = /robots.txt {
        proxy_pass https://api.my-website.com/robots.txt;
    }

    location /_next/static/ {
        alias /home/ec2-user/my-app/.next/static/;
        expires 1y;
        access_log on;
    }

    location / {
        # reverse proxy for merchant next server
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass_request_headers      on;
        proxy_cache_bypass $http_upgrade;
        proxy_buffering off;
    }
}

最佳答案

这是一个如何依赖上游 Content-Type 的示例标题设置 ExpiresCache-Control标题:

map $upstream_http_content_type $expire {
    ~^image/    1y; # 'image/*' content type
    default     off;
}
server {
    ...
    location / {
        # reverse proxy for merchant next server
        proxy_pass http://localhost:3000;
        ...
        expires $expire;
    }
}

您可以使用相同的方式为任何其他内容类型的代理响应调整缓存控制 header 。 $upstream_http_<name> nginx 变量描述 here .

更新

要仅通过特定 URI 添加缓存控制 header ,您可以使用两个链式 map block :

map $uri $expire_by_uri {
    ~^/_next/image/    1y;
    default            off;
}
map $upstream_http_content_type $expire {
    ~^image/           $expire_by_uri;
    default            off;
}

如果您除了来自 /_next/image/... 的图像之外别无所求URI,你可以只使用

map $uri $expire {
    ~^/_next/image/    1y;
    default            off;
}

关于nginx - 如何使用 NGINX 缓存 NextJS 10.0 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64972168/

相关文章:

material-ui - 在 Material UI 中使用新的 Next.js Image 组件

reactjs - 加载时如何为 Nextjs/Image 添加淡入动画?

ruby-on-rails - Sidekiq 工作人员没有被触发

python - 将本地主机 127.0.0.1 添加到 ALLOWED_HOSTS

reactjs - 如何在客户端获取Next.JS环境变量?

reactjs - Nextjs api "pages/api"在 Vercel 服务器上不起作用

nginx - 在 Ubuntu 10.04 上使用 Nginx 自动使用域名启动 Tornado

Kubernetes 上的 PHP-FPM + Nginx

reactjs - 如何使用 react Hook 表单验证密码并确认密码,是的

next.js - Next 图像组件在生产中抛出 404 错误,在开发中工作正常