Nginx 不缓存 json

Nginx 不缓存 json

我有以下 nginx 配置

user nginx;
worker_processes 10;
pid /var/run/nginx.pid;

events {
    worker_connections 1000;
}

http {

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    log_format log_cache [$time_local] $remote_addr " - " '"$request"' " - " $upstream_cache_status " " $request_time " - "  $status " " $body_bytes_sent ' "$http_referer" "$http_user_agent"';
    access_log /var/log/nginx/access.log log_cache;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";
    gzip_static on;
    gzip_comp_level 4;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/json;


    proxy_cache_path /var/nginx/cache levels=1 keys_zone=mycache:50m inactive=7d;
    proxy_cache_key $request_uri;
    proxy_store on;
    proxy_temp_path /var/nginx/tmp;
    proxy_buffering on;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;


    #include /etc/nginx/conf.d/*.conf;
    #include /etc/nginx/sites-enabled/*;


server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    # Make site accessible from http://localhost/
    server_name localhost;

    root /var/nginx/www;

    proxy_cache mycache;
    proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504 http_404;
    #proxy_cache_valid any 5m;
    #proxy_cache_valid 404 1d;

    location / {
            proxy_pass http://other:8080;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            #proxy_hide_header Cache-Control;
            #Proxy_ignore_headers Cache-Control Expires Set-Cookie;

            error_page 404 /error.html;
            error_page 500 /error.html;
            error_page 502 /error.html;
            error_page 503 /error.html;
            error_page 504 /error.html;
    }
}

}

Nginx 缓存以下请求

GET / HTTP/1.1

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Tue, 07 Jan 2014 14:16:34 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Pragma: no-cache
Expires: Tue, 07 Jan 2014 14:21:34 GMT
Cache-Control: max-age=300, must-revalidate
Last-Modified: Fri, 03 Jan 2014 18:15:48 GMT
Content-Language: en-US
Content-Encoding: gzip

但不是

GET /x.json HTTP/1.1

HTTP/1.1 200 OK
Server: nginx/1.0.15
Date: Tue, 07 Jan 2014 14:16:53 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Expires: Tue, 07 Jan 2014 14:21:53 GMT
Cache-Control: max-age=300, must-revalidate
Content-Encoding: gzip

后端标头是

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Expires: Thu, 09 Jan 2014 16:46:54 GMT
Cache-Control: max-age=300, must-revalidate
Set-Cookie: JSESSIONID=8A30B35F5BA063A2A5483189C95BFF54; Path=/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-US
Transfer-Encoding: chunked
Date: Thu, 09 Jan 2014 16:41:54 GMT

在 nginx access.log 文件中我可以看到后续请求

[07/Jan/2014:14:16:53 +0000]1.1.1.2 - "GET /x.json HTTP/1.1" - MISS 0.070 - 200 13850 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"
[07/Jan/2014:14:31:32 +0000]1.1.1.2 - "GET / HTTP/1.1" - HIT 0.000 - 200 1902 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0"

在 error.log 中我可以看到

2014/01/07 14:16:53 [crit] 24811#0: *18497 mkdir() "/var/nginx/www/x" failed (2: No such file or directory) while reading upstream, client: 1.1.1.100, server: localhost, request: "GET /x.json HTTP/1.1", upstream: "http://other.server/x.json", host: "1.1.1.3"
2014/01/07 14:16:53 [crit] 24811#0: *18497 chmod() "/var/nginx/tmp/0000008596" failed (2: No such file or directory) while reading upstream, client: 1.1.1.100, server: localhost, request: "GET /x.json HTTP/1.1", upstream: "http://other.server/x.json", host: "1.1.1.3"
2014/01/07 14:16:53 [crit] 24811#0: *18497 unlink() "/var/nginx/tmp/0000008596" failed (2: No such file or directory) while reading upstream, client: 1.1.1.100, server: localhost, request: "GET /x.json HTTP/1.1", upstream: "http://other.server/x.json", host: "1.1.1.3"

两个请求均由 Tomcat7 服务器提供服务,第一个请求使用 Spring MVC 资源,第二个请求由前端控制器生成。

答案1

缓存和非缓存响应之间的一个显著区别是Last-Modified响应标头的存在(或缺失)。缓存响应具有该标头;非缓存响应没有。Last-Modified响应标头用于nginxproxy_store指令,根据文档。由于您的配置使用了proxy_store on,也许缺少这个响应标头是罪魁祸首。

希望这可以帮助!

相关内容