Linux学习笔记(2月11日)

4.41 静态文件过期时间

让静态文件缓存在客户端的浏览器中,在没有过期之前,浏览器不需要请求静态文件,就是为了让这些图片有一个时效性。
如果服务器上图片已经做了更新,没有设置静态文件的过期时间,客户端访问到的还是旧的,如果设置了过期时间,则会重新请求。

  1. 查看静态文件的过期时间:
[root@rice01 ~]# curl -I https://avatar.csdnimg.cn/A/F/4/1_weixin_34292959.jpg
// 查看headers,-x 选项可以指定目标服务器的IP和端口
Cache-Control: max-age=31104000
// 最大年龄周期,单位为秒
[root@rice01 ~]# bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. 
31104000/3600/24
360
// https://avatar.csdnimg.cn/A/F/4/1_weixin_34292959.jpg 的过期时间为当前请求时间的360天后
  1. 配置静态文件的过期时间:
[root@rice01 ~]# vi /etc/nginx/conf.d/bbs.riceyoung.com.conf
server {
    listen       80;
    server_name  bbs.riceyoung.com;

    #charset koi8-r;

    location ~*\.(png|jpeg|gif|bmp|js|css|flv)$
    {
	expires 1d; // 将过滤的静态文件的过期时间设置为1天
	access_log off;
    }

4.42 nginx防盗链

当A网站引用了B网站的图片,这种行为就叫做盗链。防盗链就是要防止A网站去引用B网站的图片。

  1. 虚拟主机配置:
[root@rice01 conf.d]# vi bbs.riceyoung.com.conf
// 增加location:
location ~*\.(png|jpeg|jpg|gif|bmp|flv|mp3|mp4)$
    {
	valid_referers none blocked server_names *.riceyoung.com;
	if ($invalid_referer) {
		return 403;
	}
    }
  1. 测试防盗链:
[root@rice01 conf.d]# yum install -y lrzsz // 安装上传工具
[root@rice01 conf.d]# cd /data/wwwroot/bbs.riceyoung.com
[root@rice01 bbs.riceyoung.com]# rz // 上传图片
[root@rice01 bbs.riceyoung.com]# nginx -t && nginx -s reload
[root@rice01 bbs.riceyoung.com]# curl -I -x127.0.0.1:80 -e "http://www.111.com/1.txt" http://bbs.riceyoung.com/1.jpg
// -e 选项指定自定义的referer
HTTP/1.1 403 Forbidden // www.111.com并不在白名单中,所以返回403表示未引用成功
Server: nginx/1.14.2
Date: Sun, 17 Feb 2019 15:44:57 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
[root@rice01 bbs.riceyoung.com]# curl -I -x127.0.0.1:80 -e "http://blog.riceyoung.com/1.txt" http://bbs.riceyoung.com/1.jpg
HTTP/1.1 200 OK // blog.riceyoung.com在白名单中,所以引用成功
Server: nginx/1.14.2
Date: Sun, 17 Feb 2019 16:00:44 GMT
Content-Type: image/jpeg
Content-Length: 66390
Last-Modified: Wed, 12 Dec 2018 05:57:36 GMT
Connection: keep-alive
ETag: "5c10a350-10356"
Accept-Ranges: bytes

4.43-4.47 访问控制

  1. IP访问限制:

白名单:

[root@rice01 conf.d]# vi bbs.riceyoung.com.conf
server {
    listen       80;
    server_name  bbs.riceyoung.com;
    allow 127.0.0.1; // 允许127.0.0.1访问
    allow 192.168.1.0/24; // 允许192.168.1网段的的IP访问
    deny all; // 拒绝其它IP
[root@rice01 conf.d]# curl -x127.0.0.1:80 -I bbs.riceyoung.com
HTTP/1.1 200 OK // 127.0.0.1在白名单中,所以访问成功
[root@rice01 conf.d]# curl -x192.168.142.131:80 -I bbs.riceyoung.com
HTTP/1.1 403 Forbidden // 192.168.142网段的IP并不在白名单中,所以不能访问

黑名单:

[root@rice01 conf.d]# !vi
server {
    listen       80;
    server_name  bbs.riceyoung.com;
    deny 127.0.0.1; // 拒绝127.0.0.1访问
    deny 192.168.1.1; // 拒绝192.168.1.1访问
[root@rice01 conf.d]# nginx -t && nginx -s reload
[root@rice01 conf.d]# curl -x127.0.0.1:80 -I bbs.riceyoung.com
HTTP/1.1 403 Forbidden // 127.0.0.1在黑名单中,所以不能访问
[root@rice01 conf.d]# curl -x192.168.142.131:80 -I bbs.riceyoung.com
HTTP/1.1 200 OK // 192.168.142.131不在黑名单中,所以访问成功
  1. 目录访问限制:
[root@rice01 conf.d]# vi bbs.riceyoung.com.conf
// 增加location:
location ~ /admin // 对目录admin进行限制
    {
        allow 127.0.0.1; // 允许127.0.0.1访问
	    deny all; // 拒绝其他IP
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/wwwroot/bbs.riceyoung.com$fastcgi_script_name;
        include        fastcgi_params;
    }
[root@rice01 conf.d]# nginx -t && nginx -s reload
[root@rice01 conf.d]# curl -x192.168.142.131:80 -I bbs.riceyoung.com/admin
HTTP/1.1 403 Forbidden
[root@rice01 conf.d]# curl -x127.0.0.1:80 -I bbs.riceyoung.com/admin
HTTP/1.1 404 Not Found
  1. 目录下某类文件访问限制:
[root@rice01 conf.d]# vi bbs.riceyoung.com.conf
// 增加location:
location ~ .*(upload|image)/.*\.php$ // upload、image目录下的.php文件
    {
	deny all; // 禁止所有
    }
[root@rice01 conf.d]# nginx -t && nginx -s reload
[root@rice01 conf.d]# curl -x127.0.0.1:80 -I bbs.riceyoung.com/upload/abc.php
HTTP/1.1 403 Forbidden
  1. 限制user-agent
[root@rice01 conf.d]# vi bbs.riceyoung.com.conf 
// 增加if判断语句:
if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')
    {
        return 403;
    }
// 如果user-agent匹配Spider/3.0、YoudaoBot、Tomato,则限制访问
[root@rice01 conf.d]# nginx -t && nginx -s reload
[root@rice01 conf.d]# curl -I -A 'aaaaaaSpider/3.0' -x127.0.0.1:80 bbs.riceyoung.com
HTTP/1.1 403 Forbidden
// -A选项指定user-agent,-e选项指定referer,-x选项指定访问目标服务器的IP和port,-I选项只显示header信息,不显示具体的网页内容,-v选项显示详细的通信过程
  1. 限制uri
[root@rice01 conf.d]# vi bbs.riceyoung.com.conf
// 增加if判断语句:
if ($request_uri ~ (abc|123))
    {
        return 403;
    }
// 如果request_uri匹配adc、123,则限制访问
[root@rice01 conf.d]# nginx -t && nginx -s reload

测试:
在这里插入图片描述如上图所示,request_uri中并没有abc或123,所以能正常访问;
在这里插入图片描述如上图所示,request_uri中包含了123,所以被限制访问。

猜你喜欢

转载自blog.csdn.net/weixin_44527700/article/details/87508615