【原创】Java基础之Nginx缓存
1 proxy_ignore_headers
1.1 Set-Cookie
By default, nginx does not caches requests with Set-Cookie.
默认情况下:Nginx不会缓存response中带有Set-Cookie的请求;
If a header includes the “Set-Cookie” field, such a response will not be cached. Processing of one or more of these response header fields can be disabled using the proxy_ignore_headers directive.
这时需要使用proxy_ignore_headers指令;

Nginx会根据proxy_ignore_headers忽略resin返回的response中的相关的header;
proxy_ignore_header指令:

Disables processing of certain response header fields from the proxied server. The following fields can be ignored: “X-Accel-Redirect”, “X-Accel-Expires”, “X-Accel-Limit-Rate” (1.1.6), “X-Accel-Buffering” (1.1.6), “X-Accel-Charset” (1.1.6), “Expires”, “Cache-Control”, and “Set-Cookie” (0.8.44).
1.2 Cache-Control
Public 指示响应可被任何缓存区缓存。
Private 指示对于单个用户的整个或部分响应消息,不能被共享缓存处理。这允许服务器仅仅描述当用户的部分响应消息,此响应消息对于其他用户的请求无效。
no-cache 指示请求或响应消息不能缓存(HTTP/1.0用Pragma的no-cache替换)
根据什么能被缓存
no-store 用于防止重要的信息被无意的发布。在请求消息中发送将使得请求和响应消息都不使用缓存。
根据缓存超时
max-age 指示客户机可以接收生存期不大于指定时间(以秒为单位)的响应。
min-fresh 指示客户机可以接收响应时间小于当前时间加上指定时间的响应。
max-stale 指示客户机可以接收超出超时期间的响应消息。如果指定max-stale消息的值,那么客户机可以接收超出超时期指定值之内的响应消息。
经测试,cache-control为private和no-cache时也不能生成缓存文件;
也需要使用proxy_ignore_header指令:

1.3 Expires
Parameters of caching can also be set directly in the response header. This has a higher precedence than setting of caching time using the directive. The “X-Accel-Expires” header field sets caching time of a response in seconds. The value 0 disables to cache a response. If a value starts with the prefix @, it sets an absolute time in seconds since Epoch, up to which the response may be cached. If header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or “Cache-Control”.
Expires和Cache-Control会影响缓存的有效期,这个有效期默认是在配置文件中配置的:

除非确实想由resin来设置过期时间,也需要使用proxy_ignore_header指令:

经过以上三步配置后,nginx会忽略resin返回response中影响缓存是否生成以及缓存时间的header,缓存文件就可以正常生成;
2 proxy_hide_header
By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-...” from the response of a proxied server to a client. Theproxy_hide_header directive sets additional fields that will not be passed.
由于第一次访问resin,resin会返回Set-Cookie:JSESSIONID=…,而nginx会将resin的返回原封不动的存为缓存文件:

打开缓存文件发现,第一行为乱码,可能是nginx控制缓存时间相关的字节,第二行为该缓存文件的KEY,下边就是一个完整的HTTP响应,包括完整的header和body,命中缓存的时候,nginx会将该响应直接返回而不再请求resin;
为了避免串Session的问题,需要增加proxy_hide_header指令:

来保证nginx在使用缓存的同时隐藏响应中相关header;
Proxy_hide_header指令:

详细请查看 http://wiki.nginx.org/HttpProxyModule;
3 memcached module
另外,nginx还有一个memcached模块也可以用来作为页面缓存,即Nginx / memcached module:
The Nginx / Memcached module allows you to setup the previous architecture, using Nginx as a HTTP reverse proxy, and Memcached as storage. (Note: memcached is often used as shared HTTP session storage)
The module Nginx memcached works very well. But it has a big limitation: it can not store HTTP headers with data. For example, pages served by Nginx via Memcached storage have the default Nginx Content-Type. Moreover, it is quite difficult to store multiple type of data in Memcached : CSS, JS, images, HTML, json … You can add some specifics HTTP headers, but only in Nginx configuration. These headers will be shared by every resources served by Nginx/Memached, unless you put lot of ugly “if” in the configuration.
使用memcached做页面缓存的话避免了使用proxy_cache在每台nginx上都有一份缓存的问题,而且purge的时候也更简单;
【原创】Java基础之Nginx缓存的更多相关文章
- [ 原创 ] Java基础3--Java中的接口
一.使用接口(interface)的目的 Java只支持单继承,即一个类最多只能继承一个直接父类,接口的主要功能就是可以实现类似于类的多重继承的功能. 二.接口的性质 1.接口具有继承性,即子接口可继 ...
- [ 原创 ] Java基础2--构造方法的继承和重载
1.构造方法的重载是指同一个类中定义不同参数的多个构造方法,已完成不同情况下对象的初始化. 例如: Point(); Point(x); Point(x,y); 2.一个类的若干个构造方法之间可以相互 ...
- [ 原创 ] Java基础1--Java中super和this的用法和区别
许多同学在学习Java时分不清楚this和super的用法和区别,今天偶然发现一片加精的博文,看完内容准备自己也写下来积累一下 1.如果想在子类的构造方法中调用父类的构造方法,必须在子类的构造方法中使 ...
- [ 原创 ] Java基础9--final throw throws finally的区别
final修饰的类不可被继承,final修饰的方法可以被继承但不能被重写(覆盖) final用于可以声明属性和方法,分别表示属性的不可变及方法的不可覆盖.不是方法的不可继承 throw是用来明确地抛出 ...
- [ 原创 ] Java基础8--什么叫做重载
重载是在同一个类中,有多个方法名相同,参数列表不同(参数个数不同,参数类型不同),与方法的返回值无关,与权限修饰符无关,B中的参数列表和题目的方法完全一样了.
- [ 原创 ] Java基础7--Java反射机制主要提供了以下哪些功能?
AVA反射机制主要提供了以下哪些功能? 在运行时判断一个对象所属的类 在运行时构造一个类的对象 在运行时判断一个类所具有的成员变量和方法 在运行时调用一个对象的方法
- [ 原创 ] Java基础6--构造函数和抽象类的性质
构造函数的性质 // A.方法名与类名相同: // B.没有返回类型(例如return.void等):// C.不能被static.final.native.abstract和synchronized ...
- [ 原创 ] Java基础5--abstract class和interface的区别
1.含有abstract抽象修饰符的类就是抽象类.abstract 类不能创建实例对象 2.含有abstract方法的类必须定义为abstract class,abstract class类中的方法不 ...
- 【原创】这道Java基础题真的有坑!我也没想到还有续集。
前情回顾 自从我上次发了<这道Java基础题真的有坑!我求求你,认真思考后再回答.>这篇文章后.我通过这样的一个行文结构: 解析了小马哥出的这道题,让大家明白了这题的坑在哪里,这题背后隐藏 ...
随机推荐
- 【软件工程】Alpha冲刺 (5/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 学习调用中国天气网API,接近实现天气推送功能 对天气推送的形式进行讨论及重确 ...
- 计数原理,递推,求从左边能看到l个棒子,右边能看到r个棒子的方案数目
题意 有高为 1, 2, …, n 的 n 根杆子排成一排, 从左向右能看到 L 根, 从右向左能看到 R 根.求有多少种可能的排列方式. solution: 数据范围仅200,本来是往组合数学方 ...
- ArcGIS Python 保存lyr
import arcpy ##################my = arcpy.GetParameterAsText(0)mylyrfile = arcpy.GetParameterAsText( ...
- html5验证自适应
// 移动端跳转 var OS = function() { var a = navigator.userAgent, b = /(?:Android)/.test(a), d = /(?:Firef ...
- "挡位"还是"档位",究竟谁错了
http://baijiahao.baidu.com/s?id=1581395663965196858&wfr=spider&for=pc 对于“挡”与“档”两个字,我一直并没有给以太 ...
- 一百零六:CMS系统之修改邮箱功能完成
这里采用把验证码放到memcached中,所以封装一个memcached的操作 import memcache cache = memcache.Client(['127.0.0.1:11211'], ...
- vim在文件末尾增加内容
1.跳到文本的最后一行:按“G”,即“shift+g” 2.跳到最后一行的最后一个字符 : 先重复1的操作即按“G”,之后按“$”键,即“shift+4”.3 o:在当前行下面插入一个新行O:在当前 ...
- Spring Boot开发Web应用之JSP篇
前言 上一篇介绍了Spring Boot中使用Thymeleaf模板引擎,今天来介绍一下如何使用SpringBoot官方不推荐的jsp,虽然难度有点大,但是玩起来还是蛮有意思的. 正文 先来看看整体的 ...
- python3使用tkinter之Menu坑
添加菜单之后,下拉菜单的第一行是一条虚线,点击会在窗口的左上角独立显示下拉菜单,如下图所示: 去掉的方法是:创建文件菜单的时候,添加 tearoff=0参数 tearoff 有 0 和 1 两个值,分 ...
- python3 速查参考- python基础 8 -> 面向对象基础:类的创建与基础使用,类属性,property、类方法、静态方法、常用知识点概念(封装、继承等等见下一章)
基础概念 1.速查笔记: #-- 最普通的类 class C1(C2, C3): spam = 42 # 数据属性 def __init__(self, name): # 函数属性:构造函数 self ...