线上环境 不少接口 若是不作缓存 可能致使有人拿到url 每秒几万次的访问后台程序,致使系统down机.
此处, nginx能够加一层缓存. html
expires起到控制页面缓存的做用,合理的配置expires能够减小不少服务器的请求
要配置expires,能够在http段中或者server段中或者location段中加入
PS. 能够限制单个接口的访问nginx
location ~ ^/user/ {
proxy_pass http://user.example.com;web
expires 1; //1h //1d //expires max; 部分文件看须要能够永久 }缓存
nginx wiki http://nginx.org/en/docs/http/ngx_http_core_module.html#location
语法: expires [time|epoch|max|off]服务器
默认值: expires offurl
做用域: http, server, locationserver
使用本指令能够控制HTTP应答中的“Expires”和“Cache-Control”的头标,(起到控制页面缓存的做用)。htm
能够在time值中使用正数或负数。“Expires”头标的值将经过当前系统时间加上您设定的 time 值来得到。blog
epoch 指定“Expires”的值为 1 January, 1970, 00:00:01 GMT。接口
max 指定“Expires”的值为 31 December 2037 23:59:59 GMT,“Cache-Control”的值为10年。
-1 指定“Expires”的值为 服务器当前时间 -1s,即永远过时
“Cache-Control”头标的值由您指定的时间来决定:
"off" 表示不修改“Expires”和“Cache-Control”的值
demo:
控制图片等过时时间为30天,固然这个时间能够设置的更长。具体视状况而定
好比
location~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
expires 30d;
}
控制匹配/resource/或者/mediatorModule/里全部的文件缓存设置到最长时间
好比
location ~ /(resource|mediatorModule)/ {
root /opt/demo;
expires max;
}
另附其余方案: Nginx限制某个IP同一时间段的访问次数 nginx webcache配置 http://blog.sina.com.cn/s/blog_664553710100zmxr.html