AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 861565
Accepted
Tristan
Tristan
Asked: 2017-07-12 07:50:29 +0800 CST2017-07-12 07:50:29 +0800 CST 2017-07-12 07:50:29 +0800 CST

后端生成的nginx缓存图片

  • 772

我的应用程序生成动态内容,我尝试使用浏览器缓存来缓存它们。因此,如果我尝试将以下内容添加到我的服务器块:

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}

Nginx 不再将请求传递给后端以生成资产,它以 http 结束status code 404

相反,添加以下两个条件可以解决我的问题(取自这个问题):

location / {
    if ($upstream_http_content_type ~ "(image/jpeg)|(image/png)|(image/gif)|(image/svg+xml)|(text/css)|(text/javascript)|(application/javascript)") {
      expires 90d;
    }

    if ($sent_http_content_type ~ "(image/jpeg)|(image/png)|(image/gif)|(image/svg+xml)|(text/css)|(text/javascript)|(application/javascript)") {
      expires 90d;
    }
    try_files $uri @rewriteapp;
}

location @rewriteapp {
    rewrite ^(.*)$ /site.php/$1 last;
}

location ~ ^/(site|site_dev|admin|admin_dev)\.php(/|$) {
    # it's PHP that generates some of my assets
    fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS on;
}

问题是我在 http 响应标头中看不到任何表明它正在工作的日期,并且图像将从浏览器缓存中提供 90 天。

我错过了什么?

nginx
  • 1 1 个回答
  • 2575 Views

1 个回答

  • Voted
  1. Best Answer
    Jens Bradler
    2017-07-15T04:08:37+08:002017-07-15T04:08:37+08:00

    当您在 PHP 应用程序中生成 HTTP 响应时,您还应该查看有关响应标头的应用程序。浏览器缓存只是 HTTP 响应标头问题。因此,如果您的应用程序提供了正确的标头,您的 Web 服务器将为它们提供服务,您的浏览器将缓存结果。

    但是,您可以在 Web 服务器中覆盖此行为。但在 Web 服务器级别执行此操作绝不是一个好主意。在这里,您只有 URL 和请求标头来决定是否应缓存响应。您的应用程序通常有更多细节来做出更好的决定。

    最好的方法是通过发送适当的缓存标头来修复您的 PHP 代码:

    <?php
    ...
    header('Cache-Control: max-age='.90*24*3600);
    

    对于替代方法,您可以(但不应该)在 Nginx 配置中使用它来覆盖 Web 应用程序的响应:

    location ~ ^/(site|site_dev|admin|admin_dev)\.php(/|$) {
        # it's PHP that generates some of my assets
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
    
        # hide response header sent by PHP
        fastcgi_hide_header Cache-Control;
        fastcgi_hide_header Expires;
        # set Cache-Control response for client
        expires 90d;
    }
    

    更好的方法是在 Web 服务器级别进行缓存。在这里,您可以配置异常并能够对 HTTP 响应中的“Set-Cookie”标头等特殊情况做出反应。

    顺便说一句,新访客也将获得快速交付的优势。基于浏览器的缓存对首次访问者没有帮助。

    location ~ ^/(site|site_dev|admin|admin_dev)\.php(/|$) {
        # it's PHP that generates some of my assets
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS on;
    
        # activate caching (you must define a cache storage for phpfpm)
        fastcgi_cache          phpfpm;
        # enable caching for 90 days for responses with HTTP status 200
        fastcgi_cache_valid    200 90d;
        # allow headers from PHP to client
        fastcgi_pass_header    Set-Cookie;
        fastcgi_pass_header    Cookie;
        # ignore headers from PHP to client
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        # remove headers from PHP to client
        fastcgi_hide_header    Cache-Control;
        fastcgi_hide_header    Expires;
        # don't save in cache
        fastcgi_no_cache       $cookie_PHPSESSID;
        # don't serve from cache
        fastcgi_cache_bypass   $cookie_PHPSESSID;
    }
    

    请注意,这只是基于标准设置的建议。它可能因您的设置而异。

    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve