您好,欢迎访问一九零五行业门户网

Nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链

这篇文章给大家介绍的内容是关于nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
一、控制浏览器缓存1. 浏览器缓存简介浏览器缓存遵循http协议定义的缓存机制(如:expires;cache-control等)。
当浏览器无缓存时,请求响应流程
当浏览器有缓存时,请求响应流程
浏览器缓存校验过期机制校验是否过期cache-control(max-age)、expires
协议中etag头信息校验 etag
last-modified头信息校验 last-modified
浏览器请求流程
2. nginx控制浏览器缓存配置nginx通过添加cache-control(max-age)、expires头信息的方式控制浏览器缓存。ngx_http_headers_module语法syntax:    expires [modified] time;        expires epoch | max | off;default:    expires off;context:    http, server, location, if in location
本配置项可以控制http响应中的“expires”和“cache-control”头信息,(起到控制页面缓存的作用)。
“expires”头信息中的过期时间为当前系统时间与您设定的 time 值时间的和。如果指定了 modified 参数,则过期时间为文件的最后修改时间与您设定的 time 值时间的和。
“cache-control”头信息的内容取决于指定 time 的符号。可以在time值中使用正数或负数。
当 time 为负数,“cache-control: no-cache”;
当 time 为正数或0,“cache-control: max-age=time”,单位是秒。
epoch 参数用于指定“expires”的值为 1 january, 1970, 00:00:01 gmt。
max 参数用于指定“expires”的值为 “thu, 31 dec 2037 23:55:55 gmt”,“cache-control” 的值为10 年。
off 参数令对“expires” 和 “cache-control”响应头信息的添加或修改失效。
3. 应用实例1. vim /etc/nginx/conf.d/static.confserver {    location ~ .*\.(txt|xml)$ {        # 设置过期时间为1天        expires 1d;        root /vagrant/doc;    }}
2. nginx -s reload 重新载入nginx配置文件3. 创建 /vagrant/doc/hello.txt 文件4. 通过curl访问 192.168.33.88/hello.txt,查看http响应头信息[root/etc/nginx]# curl -i 192.168.33.88/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: tue, 17 jul 2018 07:12:11 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-cexpires: wed, 18 jul 2018 07:12:11 gmtcache-control: max-age=86400accept-ranges: bytes
重点查看 expires 和 cache-control两个字段,可见,hello.txt 的缓存时间为1天。
二、防盗链目的:防止资源被盗用
思路:区别哪些请求是非正常的用户请求
1. 基于http_refer防盗链配置模块ngx_http_referer_module语法syntax:    valid_referers none | blocked | server_names | string ...;default:    —context:    server, location
none:请求头中没有 referer 字段
blocked:请求头中虽然存在“referer”字段,但是它的值已经被防火墙或代理服务器删除;这些值是不以“http://”或“https://”开头的字符串;
server_names:“referer”请求头字段包含该服务器名称
任意字符串:定义一个服务器名称和一个可选的uri前缀。服务器名开始或结尾可以有 “*” 。检查时,“referer”字段中的服务器端口会被忽略。
正则表达式:字符串必须以 ~ 开头,值得注意的是,正则表达式匹配的是在“http://”或“https://”之后的内容。
示例valid_referers none blocked server_names *.example.com example.* www.example.org/galleries/ ~\.google\.;
2. 应用实例1. vim conf.d/static.confserver {    location ~ .*\.(txt|xml)$ {                # 配置防盗链规则        valid_referers none blocked 192.168.1.110 *.example.com example.* ~\.google\.;        # 如果不符合防盗链规则,则返回403        if ($invalid_referer) {            return 403;        }        root /vagrant/doc;    }}
2. nginx -s reload 重新载入nginx配置文件3. 创建 /vagrant/doc/hello.txt 文件vim /vagrant/a/a.txt
hello world!
4. 使用 curl进行访问测试不带referer,可以正常访问
[root~]# curl -i http://127.0.0.1/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: fri, 03 aug 2018 01:34:12 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-caccept-ranges: bytes
referer为 http://www.baidu.com,返回403
[root~]# curl -e http://www.baidu.com -i http://127.0.0.1/hello.txthttp/1.1 403 forbiddenserver: nginx/1.14.0date: fri, 03 aug 2018 01:34:34 gmtcontent-type: text/htmlcontent-length: 169connection: keep-alive
referer为 http://192.168.1.110,可以正常访问
[root~]# curl -e http://192.168.1.110 -i http://127.0.0.1/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: thu, 02 aug 2018 11:31:51 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-caccept-ranges: bytes

referer以 example.开头或 .example.com 结尾,可以正常访问
[root~]# curl -e http://www.example.com -i http://127.0.0.1/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: thu, 02 aug 2018 11:33:47 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-caccept-ranges: bytes[root~]# curl -e http://example.baidu.com -i http://127.0.0.1/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: thu, 02 aug 2018 11:33:53 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-caccept-ranges: bytes
referer为 http://192.168.1.110,可以正常访问
[root~]# curl -e http://192.168.1.110 -i http://127.0.0.1/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: thu, 02 aug 2018 11:31:51 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-caccept-ranges: bytes

referer为 http://google.com,返回403
[root~]# curl -e http://google.com -i http://127.0.0.1/hello.txthttp/1.1 403 forbiddenserver: nginx/1.14.0date: thu, 02 aug 2018 11:37:43 gmtcontent-type: text/htmlcontent-length: 169connection: keep-alive
referer为 http://www.google.com,可以正常访问
[root~]# curl -e http://www.google.com -i http://127.0.0.1/hello.txthttp/1.1 200 okserver: nginx/1.14.0date: thu, 02 aug 2018 11:37:50 gmtcontent-type: text/plaincontent-length: 12last-modified: tue, 17 jul 2018 07:07:22 gmtconnection: keep-aliveetag: 5b4d95aa-caccept-ranges: bytes
相关文章推荐:
nginx作为静态资源web服务并进行静态资源压缩
以上就是nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链的详细内容。
其它类似信息

推荐信息