失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链

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

时间:2019-03-03 09:53:48

相关推荐

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

后端开发|php教程

php,linux,nginx,webserver,运维

后端开发-php教程

这篇文章给大家介绍的内容是关于Nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链 ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

android 人脸美化源码,内网vscode中文版安装教程,ubuntu 查看源,tomcat的网站目录结构,凶残的爬虫,php点赞功能代码,宁波seo怎么做推广软件lzw

A、控制浏览器缓存

php 图书管理系统源码,vscode连接不上虚拟机,ubuntu系统wifi,tomcat安装完闪退,爬虫手机信息,php系统源码分析,手机seo搜索引擎排名lzw

1. 浏览器缓存简介

浏览器缓存遵循HTTP协议定义的缓存机制(如:Expires;Cache-control等)。

照片墙全可用户上传 源码,vscode 字符串,ubuntu源apt,tomcat中文api,sqlite建表字段约束,前端只需要学一个框架吗,爬虫从入门到入狱图片卡通,php读取数据乱码,偃师seo优化设计,在线视频教学网站模版,安装wordpress 此网页包含重定向循环,phpcms模板工具lzw

当浏览器无缓存时,请求响应流程

当浏览器有缓存时,请求响应流程

浏览器缓存校验过期机制

浏览器请求流程

2. Nginx控制浏览器缓存配置

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.conf

server { 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 07:12:11 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Expires: Wed, 18 Jul 07:12:11 GMTCache-Control: max-age=86400Accept-Ranges: bytes

重点查看ExpiresCache-Control两个字段,可见,hello.txt 的缓存时间为1天。

B、防盗链

目的:防止资源被盗用

思路:区别哪些请求是非正常的用户请求

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.* /galleries/ ~\.google\.;

2. 应用实例

1. vim conf.d/static.conf

server { location ~ .*\.(txt|xml)$ {# 配置防盗链规则 valid_referers none blocked 192.168.1.110 *. 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 01:34:12 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Accept-Ranges: bytes

referer为,返回403

[root~]# curl -e "" -I http://127.0.0.1/hello.txtHTTP/1.1 403 ForbiddenServer: nginx/1.14.0Date: Fri, 03 Aug 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 11:31:51 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Accept-Ranges: bytes

referer以example.开头或.结尾,可以正常访问

[root~]# curl -e "" -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 11:33:47 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Accept-Ranges: bytes[root~]# curl -e "" -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 11:33:53 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Accept-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 11:31:51 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Accept-Ranges: bytes

referer为,返回403

[root~]# curl -e "" -I http://127.0.0.1/hello.txtHTTP/1.1 403 ForbiddenServer: nginx/1.14.0Date: Thu, 02 Aug 11:37:43 GMTContent-Type: text/htmlContent-Length: 169Connection: keep-alive

referer为,可以正常访问

[root~]# curl -e "" -I http://127.0.0.1/hello.txtHTTP/1.1 200 OKServer: nginx/1.14.0Date: Thu, 02 Aug 11:37:50 GMTContent-Type: text/plainContent-Length: 12Last-Modified: Tue, 17 Jul 07:07:22 GMTConnection: keep-aliveETag: "5b4d95aa-c"Accept-Ranges: bytes

相关文章推荐:

Nginx作为静态资源web服务并进行静态资源压缩

如果觉得《Nginx作为静态资源web服务来控制浏览器缓存以及实现防盗链》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。