caching - 服务静态内容 NGINX

标签 caching nginx proxy

我是新来的 NGINX .我对此知之甚少,但我正在努力。
我很好奇使用 NGINX 从我的页面提供静态内容的最佳方式是什么。我想提供静态内容的主要原因是我想减少应用服务器的负载,并提高页面加载速度。

我碰到
几篇好文章可以帮助我将这些文章放在一起:here , here , here , 和 here .
但一切还是有点清楚。

配置
文件路径:etc/nginx/default

server {

    listen 80 default_server;
    server_name default;
    root /home/forge/site/public;

    location / {
        proxy_pass http://43.35.49.160/;
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Media: images, icons, video, audio, HTC
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
    }

    # CSS and Javascript
    location ~* \.(?:css|js)$ {
        expires 1y;
        access_log off;
        add_header Cache-Control "public";
    }
}

测试结果
保存文件后,我运行 service nginx reload .
接下来,我尝试运行:curl -X GET -I http://45.33.69.160/index.php我有:
HTTP/1.1 200 OK
Server: nginx/1.6.3
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Date: Fri, 08 May 2015 15:14:55 GMT
Set-Cookie: XSRF-TOKEN=eyJpdiI6IkhPa2kwK1wvd2kxMFV0TURzYnMwSXFnPT0iLCJ2YWx1ZSI6IkFpSFpvakNjcGp0b0RWcVViYXJcLzRHbmo3XC9qbStYc2VzYVh4ZHVwNW45UGNQMmltZEhvSys1NjhZVzZmckhzOGRBUk5IU1pGK084VDF1ZmhvVkZ4MlE9PSIsIm1hYyI6IjliMzc5NWQ4MWRiMjM1NzUxNjcyNGNmYWUzMGQyMDk3MjlkYTdhYzgxYTI0OGViODhlMTRjZTI4MWE5MDU2MGYifQ%3D%3D; expires=Fri, 08-May-2015 17:14:55 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=eyJpdiI6Iklhb041MkVBak0rVm5JeUZ0VVwvZ3pnPT0iLCJ2YWx1ZSI6IitRUFlzQzNmSm1FZ0NQVVFtaTJ4cG1hODlDa2NjVDgzdXBcLzRcL0ZSM1ZPOTRvRGo5QjQ1REluTUM3Vjd3cFptV3dWdHJweTY3QW5QR2lwTkZMUlNqbnc9PSIsIm1hYyI6IjIxOTZkYzM5ODE0N2E4YmQzODMxZGYzMDY3NjI4ODM1YWQxNGMxNDRlZDZmMGE1M2IwZWY2OTU4ZmVjOTIyMjkifQ%3D%3D; expires=Fri, 08-May-2015 17:14:55 GMT; Max-Age=7200; path=/; httponly
然后,我尝试运行 curl -X GET -I http://45.33.69.160/css/custom.css我有 :
HTTP/1.1 200 OK
Server: nginx/1.6.3
Date: Fri, 08 May 2015 15:16:03 GMT
Content-Type: text/css
Content-Length: 2890
Last-Modified: Thu, 07 May 2015 03:02:38 GMT
Connection: keep-alive
ETag: "554ad5ce-b4a"
Accept-Ranges: bytes
为什么我看到 Cache-Control: no-cache而我只是设置了缓存?
现在一切对我来说都不清楚。

问题
有人可以说清楚如何:
  • 正确配置
  • 测试该配置是否有效
  • 查看缓存和不缓存的区别
  • 对其进行基准测试并在页面或 CLI 上打印该报告

  • ?

    最佳答案

    Cache-Control: no-cache
    this answer about no-cache 中所述,链接到 the spec , Cache-Control: no-cache应该告诉用户代理和中间缓存使用哪种缓存样式(即每次与服务器重新验证)。这适用于您专门使用 nginx 的情况。如果您将其用作传递,您 need to set proxy_ignore_headers , 喜欢

    proxy_ignore_headers Cache-Control;
    

    配置

    除此之外:在NGINX reference about content caching ,它说把线
        proxy_cache_path /data/nginx/cache keys_zone=one:10m;
    

    http部分,然后是
            proxy_cache one;
    

    server部分。

    测试

    In this SF question ,它说通过添加 X-Cache-Status 来测试缓存行为通过配置文件 header
    add_header X-Cache-Status $upstream_cache_status;
    

    Its answer指出

    You can view headers with

    • the Firefox addon firebug
    • the Chrome debugging console
    • cURL (curl -I )

    关于caching - 服务静态内容 NGINX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30128086/

    相关文章:

    docker - 如何在 docker 中安装 nginx 1.14.X?

    nginx 和 apache 上的 Django : where to handle 404 & 500 error?

    php - 服务器恢复后 502 Bad Gateway Nginx

    spring-boot - Zuul 通过外部代理服务器路由请求

    sqlite - 未捕获的TypeError:对象app.data.proxy.CustomSql没有方法“应用” sencha touch

    hibernate - 当lazy=true 时,如何让equals() 与Hibernate 实体一起工作?

    apache - htaccess mod_headers 用于无缓存

    mysql - 更新 MySQL 中的缓存计数

    asp.net-mvc - 在 Web API c# MVC 中使用 HttpActionExecutedContext 时出现缓存问题

    c# - 获取集合的缓存模式