我有以下nginxconfiguration
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; } } }
Nginxcaching下面的请求
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资源,第二个由前端控制器生成。
高速caching和非高速caching响应之间的一个显着差异是Last-Modified响应头的存在(或缺less)。 caching的响应具有该标题; 非高速caching的响应不会。 Last-Modified响应头由nginx用于其proxy_store指令,根据文档 。 由于你的configuration使用了proxy_store on ,可能缺less这个响应头是罪魁祸首。
希望这可以帮助!