Nginx没有缓存我的应用程序

时间:2016-11-22 07:56:02

标签: node.js express caching nginx

我正在尝试使用Nginx设置我的ExpressApp及其呈现的Jade文件的缓存。

当我在运行谷歌Pagespeed Insight测试时查看我的应用程序的pm2日志,看到图像和HTML仍然由应用程序而不是Nginx代理服务。

两个问题:

  1. 如何更改配置以获得所需效果?
  2. 如何有效验证安装/缓存是否正常工作?
  3. 谢谢!

    我目前的Nginx配置:

    proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=backcache:8m max_size=50m;
    proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 404 1m;
    
    server {
            listen 80 default_server;
            listen [::]:80 default_server;
    
            location / {
    
                     expires 1w;
                     proxy_cache backcache;
                     #proxy_cache_bypass $http_cache_control;
                     add_header X-Proxy-Cache $upstream_cache_status;
    
                     proxy_pass http://localhost:3030;
             } 
    }
    

    接头:

    * TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384
    * Server certificate: ###
    * Server certificate: COMODO RSA Domain Validation Secure Server CA
    * Server certificate: COMODO RSA Certification Authority
    * Server certificate: AddTrust External CA Root
    > GET / HTTP/1.1
    > Host: ###
    > User-Agent: curl/7.43.0
    > Accept: */*
    >
    < HTTP/1.1 200 OK
    < Server: nginx/1.10.0 (Ubuntu)
    < Date: Thu, 24 Nov 2016 13:15:37 GMT
    < Content-Type: text/html; charset=utf-8
    < Content-Length: 66761
    < Connection: keep-alive
    < Vary: Accept-Encoding
    < X-Powered-By: Express
    < Access-Control-Allow-Origin: *
    < Access-Control-Allow-Methods: GET,PUT,POST,DELETE
    < Access-Control-Allow-Headers: Origin, x-access-token, X-Requested-With, Content-Type, Accept, un-populated
    < ETag: W/"104c9-8+4lo531tUk9k3SJq4sPHg"
    < Expires: Thu, 01 Dec 2016 13:15:37 GMT
    < Cache-Control: max-age=604800
    < X-Proxy-Cache: MISS
    

    对图像的两个连续请求的标头

    ➜  ~ curl -I **url/img.jpg**
    HTTP/1.1 200 OK
    Server: nginx/1.10.0 (Ubuntu)
    Date: Fri, 25 Nov 2016 03:17:02 GMT
    Content-Type: application/octet-stream
    Content-Length: 11007
    Connection: keep-alive
    X-Powered-By: Express
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: GET,PUT,POST,DELETE
    Access-Control-Allow-Headers: Origin, x-access-token, X-Requested-With, Content-Type, Accept, un-populated
    Accept-Ranges: bytes
    Cache-Control: max-age=604800
    Last-Modified: Thu, 17 Nov 2016 16:23:13 GMT
    ETag: W/"2aff-158731964ca"
    Expires: Fri, 02 Dec 2016 03:17:02 GMT
    X-Proxy-Cache: MISS
    
    ➜  ~ curl -I **url/img.jpg**
    HTTP/1.1 200 OK
    Server: nginx/1.10.0 (Ubuntu)
    Date: Fri, 25 Nov 2016 03:17:08 GMT
    Content-Type: application/octet-stream
    Content-Length: 11007
    Connection: keep-alive
    X-Powered-By: Express
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Methods: GET,PUT,POST,DELETE
    Access-Control-Allow-Headers: Origin, x-access-token, X-Requested-With, Content-Type, Accept, un-populated
    Accept-Ranges: bytes
    Cache-Control: max-age=604800
    Last-Modified: Thu, 17 Nov 2016 16:23:13 GMT
    ETag: W/"2aff-158731964ca"
    Expires: Fri, 02 Dec 2016 03:17:08 GMT
    X-Proxy-Cache: MISS
    

1 个答案:

答案 0 :(得分:0)

您显示为请求标头的标头很可能不是原始标头,因为它显示“显示临时标头”。

实际请求标头可能包含“cache-control”标头,您的nginx配置使用该标头绕过缓存,如“X-Proxy-Cache:BYPASS”响应标头所示。

如果没有理由保留它,则应从配置中删除proxy_cache_bypass - 指令。