Nginx não faz cache de json

1

Eu tenho a seguinte configuração 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;
    }
}

}

O Nginx armazena em cache o seguinte pedido

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

mas não

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

Os cabeçalhos de back-end são

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

No arquivo nginx access.log, posso ver nas solicitações subsequentes

[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"

No error.log eu posso ver

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"

Ambas as solicitações são atendidas por um servidor Tomcat7, primeiro usando recursos do Spring MVC, o segundo gerado pelo front controller.

    
por Adrian Ber 07.01.2014 / 15:36

1 resposta

1

Uma diferença notável entre as respostas em cache e não em cache é a presença (ou falta) do cabeçalho de resposta Last-Modified . A resposta em cache tem esse cabeçalho; a resposta não armazenada em cache não. O cabeçalho de resposta Last-Modified é usado por nginx para a diretiva proxy_store , de acordo com a documentação . Como sua configuração usa proxy_store on , talvez a falta desse cabeçalho de resposta seja o culpado.

Espero que isso ajude!

    
por 12.02.2016 / 23:27

Tags