一、控制浏览器缓存一、浏览器缓存简介浏览器缓存遵循HTTP协议定义的缓存机制(如:过期;缓存控制等)。当浏览器没有缓存时,请求响应过程当浏览器有缓存时,请求响应过程浏览器缓存检查过期机制,检查是否过期Cache-Control(max-age),检查中的Etag头信息Expires协议Modified头信息验证Last-Modified浏览器请求流程2.Nginx控制浏览器缓存配置Nginx通过添加Cache-Control(max-age)和Expires头信息来控制浏览器缓存。ngx_http_headers_module语法:过期[修改]时间;过期纪元|最大|离开;默认:过期关闭;context:http,server,location,ifinlocation这个配置项可以控制HTTP响应中的"Expires"和"Cache-Control"Headerinformation,(起到控制页面缓存的作用)。"中的过期时间expires”头是当前系统时间和你设置的时间值之和。如果指定了修改参数,则过期时间是文件的最后修改时间和你设置的时间值之和。内容“Cache-Control”头取决于指定时间的交易品种。时间值可以使用正数或负数。当时间为负数时,“Cache-Control:no-cache”;当时间为正数或0时,“Cache-Control:max-age=time”,单位是秒。epoch参数用于指定“Expires”值为1January,1970,00:00:01GMT。max参数用于指定该值“Expires”的值为“Thu,31Dec203723:55:55GMT”,“Cache-Control”的值为10年。off参数禁用对“Expires”和“Cache-Control”的添加或修改响应头。三、应用示例1.vim/etc/nginx/conf.d/static.confserver{location~.*\.(txt|xml)${#设置过期时间为1天expires1d;根/流浪者/文档;}}2。nginx-sreload重新加载nginx配置文件3.创建/vagrant/doc/hello.txt文件4.通过curl访问192.168.33.88/hello.txt查看http响应头信息[root/etc/nginx]#curl-I192.168.33.88/hello.txtHTTP/1.1200OKServer:nginx/1.14.0日期:2018年7月17日星期二07:12:11GMTContent-Type:text/plainContent-Length:12Last-Modified:2018年7月17日星期二07:07:22GMTConnection:keep-aliveETag:"5b4d95aa-c"Expires:Wed,18Jul201807:12:11GMTCache-Control:max-age=86400Accept-Ranges:bytes关注Expires和Cache-Control字段,可以看到hello.txt的缓存时间为1天。二、防盗链目的:防止资源被盗思路:区分哪些请求是异常用户请求1、基于http_refer的防盗链配置模块ngx_http_referer_modulesyntaxSyntax:valid_referersnone|阻止|服务器名称|,locationnone:请求头中没有Referer字段。blocked:虽然请求头中存在“Referer”字段,但其值已被防火墙或代理服务器删除;这些值不以“http://”或“https://”开头;server_names:“Referer”请求头字段包含服务器名称的任意字符串:定义一个服务器名称和一个可选的URI前缀。服务器名称可以以“*”开头或结尾。检查时,忽略“Referer”字段中的服务器端口。正则表达式:字符串必须以~开头。值得注意的是,正则表达式匹配“http://”或“https://”之后的内容。示例valid_referersnoneblockedserver_names*.example.comexample.*www.example.org/galleries/~\.google\.;2.应用示例1.vimconf.d/static.confserver{location~.*\.(txt|xml)${#配置防盗链规则valid_referersnoneblocked192.168.1.110*.example.comexample.*~\.谷歌\。;#如果不满足防盗链规则,则返回403if($invalid_referer){return403;}根/流浪者/文档;}}2.nginx-sreload重新加载nginx配置文件3.创建/vagrant/doc/hello.txt文件vim/vagrant/a/a.txtHelloworld!4.使用curl进行无referer访问测试,可以正常访问[root~]#curl-Ihttp://127.0.0.1/hello.txtHTTP/1.1200OKServer:nginx/1.14.0Date:Fri,032018年8月01:34:12GMTContent-Type:text/plainContent-Length:12Last-Modified:Tue,17Jul201807:07:22GMTConnection:keep-aliveETag:"5b4d95aa-c"Accept-Ranges:bytesrefererishttp://www.baidu.com,return403[root~]#curl-e"http://www.baidu.com"-Ihttp://127.0.0.1/hello.txtHTTP/1.1403ForbiddenServer:nginx/1.14.0日期:8月3日,星期五201801:34:34GMTContent-Type:text/htmlContent-Length:169Connection:keep-alivereferer为http://192.168.1.110,可以正常访问[root~]#curl-e"http://192.168.1.110“-Ihttp://127.0.0.1/hello.txtHTTP/1.1200OKServer:nginx/1.14.0Date:Thu,02Aug201811:31:51GMTContent-Type:text/plainContent-Length:12Last-Modified:Tue,2018年7月17日07:07:22GMTConnection:keep-aliveETag:“5b4d95aa-c”Accept-Ranges:bytesreferer以示例开头。或者以.example.com结尾,可以正常访问[root~]#curl-e"http://www.example.com"-Ihttp://127.0.0.1/hello.txtHTTP/1.1200OKServer:nginx/1.14.0日期:2018年8月2日星期四11:33:47GMTContent-Type:text/plainContent-Length:12Last-Modified:2018年7月17日星期二07:07:22GMTConnection:keep-aliveETag:“5b4d95aa-c”Accept-Ranges:bytes[root~]#curl-e"http://example.baidu.com"-Ihttp://127.0.0.1/hello.txtHTTP/1.1200OKServer:nginx/1.14.0Date:Thu,2018年8月2日11:33:53GMTContent-Type:text/plainContent-Length:12Last-Modified:星期二,2018年7月17日07:07:22GMTConnection:keep-aliveETag:"5b4d95aa-c"Accept-Ranges:bytesreferer为http://192.168.1.110,可以正常访问[root~]#curl-e"http://192.168.1.110"-Ihttp://127.0.0.1/hello.txtHTTP/1.1200OKServer:nginx/1.14.0日期:2018年8月2日星期四11:31:51GMTContent-Type:text/plainContent-Length:12Last-Modified:2018年7月17日星期二07:07:22GMTConnection:keep-aliveETag:“5b4d95aa-c”接受范围:bytesreferer是http://google.com,返回403[root~]#curl-e“http://google.com”-Ihttp://127.0.0.1/hello.txtHTTP/1.1403ForbiddenServer:nginx/1.14.0Date:Thu,02Aug201811:37:43GMTContent-Type:text/htmlContent-Length:169Connection:keep-aliverefererishttp://www.google.com可以正常访问[root~]#curl-e"http://www.google.com"-Ihttp://127.0.0.1/hello.txtHTTP/1.1200OKServer:nginx/1.14.0Date:2018年8月2日星期四11:37:50GMTContent-Type:text/plainContent-Length:12Last-Modified:2018年7月17日星期二07:07:22GMTConnection:keep-aliveETag:“5b4d95aa-c"接受范围:字节
