Nginx配置静态资源缓存时间及实现防盗链

  • 环境
    源主机:192.168.10.158
    系统:centos 7.4
    域名:www.wuxier.cn
    盗链主机:192.168.10.191(使用Nginx+Tomcat实现负载均衡、动静分离的实验主机,点我进行复盘
    系统:centos 7.4
    域名:www.ajie.com 和 www.taobao.com
  • 创建软件包存放目录
  • [[email protected] ~]# mkdir /root/software
    [[email protected] ~]# cd /root/software/
    [[email protected] software]# rz
    rz waiting to receive.
    Starting zmodem transfer.  Press Ctrl+C to cancel.
    Transferring jdk-8u181-linux-x64.tar.gz...
      100%  181295 KB    7882 KB/sec    00:00:23       0 Errors
    Transferring apache-tomcat-8.5.32.tar.gz...
      100%    9360 KB    9360 KB/sec    00:00:01       0 Errors  
    
    [[email protected] software]# 
    • 解压JDK到/user/local/
    [[email protected] software]# tar xzvf jdk-8u181-linux-x64.tar.gz -C /usr/local/
    • 查看JAVA是否安装成功
    [[email protected] software]# cd /usr/local/jdk1.8.0_181/bin/
    [[email protected] bin]# ./java -version
    java version "1.8.0_181"
    Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
    Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
    [[email protected] bin]# 
    • 配置环境变量
    [[email protected] bin]# vi /etc/profile  #新增下面三行
    export JAVA_HOME=/usr/local/jdk1.8.0_181
    export CLASSPATH=.:$JAVA_HOME/lib
    export PATH=$PATH:$JAVA_HOME/bin
    
    [[email protected] bin]# source /etc/profile
    • 关闭selinux
    [[email protected] bin]# getenforce
    Enforcing
    [[email protected] bin]# setenforce 0
    [[email protected] bin]# getenforce
    Permissive
    • 安装Nginx
    [[email protected] bin]# cd /etc/yum.repos.d/
    [[email protected] yum.repos.d]# vim nginx.repo
    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/7/$basearch/
    gpgcheck=0
    enabled=1
    
    [[email protected] yum.repos.d]# yum install nginx
    [[email protected] yum.repos.d]# systemctl start nginx
    [[email protected] yum.repos.d]# systemctl enable nginx

    • 隐藏Nginx版本号
    [[email protected] ~]# curl -I http://192.168.10.158
    HTTP/1.1 200 OK
    Server: nginx/1.14.0  #版本号
    Date: Thu, 23 Aug 2018 02:22:14 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 17 Apr 2018 15:48:00 GMT
    Connection: keep-alive
    ETag: "5ad61730-264"
    Accept-Ranges: bytes
    
    [[email protected] ~]# vi /etc/nginx/nginx.conf
         21     server_tokens off;  #新增
         22     access_log  /var/log/nginx/access.log  main;
    
    [[email protected] ~]# systemctl restart nginx
    [[email protected] ~]# curl -I http://192.168.10.158
    HTTP/1.1 200 OK
    Server: nginx  #版本号隐藏了
    Date: Thu, 23 Aug 2018 02:24:46 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Tue, 17 Apr 2018 15:48:00 GMT
    Connection: keep-alive
    ETag: "5ad61730-264"
    Accept-Ranges: bytes
    • 在Nginx首页添加一张图片
    [[email protected] conf.d]# cd /usr/share/nginx/html/
    [[email protected] html]# rz
    rz waiting to receive.
    Starting zmodem transfer.  Press Ctrl+C to cancel.
    Transferring wuxier.jpg...
      100%       5 KB       5 KB/sec    00:00:01       0 Errors  
    
    [[email protected] html]# cp index.html index.html.bak
    [[email protected] html]# vi index.html  #在首页中添加刚上传的图片
         13 <body>
         14 <img src=http://192.168.10.158/wuxier.jpg>  #新增,wuxier.jpg就是刚上传的图片
         15 <h1>Welcome to nginx!</h1>
         16 <p>If you see this page, the nginx web server is successfully installed and
         17 working. Further configuration is required.</p>
         18
         19 <p>For online documentation and support please refer to
         20 <a href="http://nginx.org/">nginx.org</a>.<br/>
         21 Commercial support is available at
         22 <a href="http://nginx.com/">nginx.com</a>.</p>
         23
         24 <p><em>Thank you for using nginx.</em></p>
         25 </body>
    • 配置静态资源缓存时间
    [[email protected] html]# vim /etc/nginx/conf.d/default.conf  #新增以下内容
        location ~ .*\.(gif|jpg|png|jpeg|bmp|ico|css)$ {
            root   /usr/share/nginx/html;
            expires 2d;
    }
    [[email protected] html]# systemctl restart nginx 

    • 修改windows的hosts文件
    • 修改虚拟主机 www.wuxier.cn 的配置文件
    [[email protected] conf.d]# pwd
    /etc/nginx/conf.d
    [[email protected] conf.d]# cp default.conf wuxier.conf
    [[email protected] conf.d]# ll
    total 8
    -rw-r--r--. 1 root root 1206 Aug 23 10:53 default.conf
    -rw-r--r--. 1 root root  283 Aug 23 12:12 wuxier.conf
    
    [[email protected] conf.d]# cat wuxier.conf
    server {
        listen       80;
        server_name  www.wuxier.cn;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        location ~ .*\.(gif|jpg|png|jpeg|bmp|ico|css)$ {
            root   /usr/share/nginx/html;
            expires 2d;
        }
    
    }
    [[email protected] conf.d]# systemctl restart nginx 

    • 验证
    • 当访问的是taobao1时,如下图


    • 当访问的是taobao2时,如下图

    • 源主机防盗链配置
    [[email protected] conf.d]# cat wuxier.conf
    server {
        listen       80;
        server_name  www.wuxier.cn;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
    #    location ~ .*\.(gif|jpg|png|jpeg|bmp|ico|css)$ {
    #        root   /usr/share/nginx/html;
    #        autoindex on;
    #        expires 2d;
    #    }
    
        location ~*\.(jpg|png|gif|jpeg)$ {
               root  /usr/share/nginx/html;  #图片路径
               valid_referers none blocked  *.wuxier.cn  wuxier.cn  *.ajie.com  ajie.com;  #可以访问图片的白名单
               if ($invalid_referer) {  #如果来路不是指定的白名单来路,则返回下面的图片
               rewrite ^/ https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535013233040&di=64a20c24bd1e4906ad2eb7205fe3abec&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fexp%2Fw%3D480%2Fsign%3D7953092ec195d143da76e52b43f18296%2F8ad4b31c8701a18bbc22f762972f07082938fed6.jpg;
                    }
                    }
    
    }
    [[email protected] conf.d]# 
    [[email protected] html]# systemctl restart nginx
    • 防盗链结果验证
    • 当访问taobao1的时候,因为taobao1之前是使用了 www.wuxier.cn/wuxier.jgp 图片,所以会返回盗链的图片,如下图

    • 当访问taobao2的时候,如下图

    • 当访问 www.wuxier.cn 的时候,如下图(白名单)

    • 当访问 www.ajie.com 的时候,如下图(白名单)

    • 当从配置文件wuxier.conf中将*ajie.com和ajie.com从白名单中删除后,再进行访问
    [[email protected] conf.d]# vim /etc/nginx/conf.d/wuxier.conf
    server {
        listen       80;
        server_name  www.wuxier.cn;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
    #    location ~ .*\.(gif|jpg|png|jpeg|bmp|ico|css)$ {
    #        root   /usr/share/nginx/html;
    #        autoindex on;
    #        expires 2d;
    #    }
    
        location ~*\.(jpg|png|gif|jpeg)$ {
               root  /usr/share/nginx/html;
               valid_referers none blocked  *.wuxier.cn  wuxier.cn;  #将*.ajie.com和ajie.com删除
               if ($invalid_referer) {
               rewrite ^/ https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535013233040&di=64a20c24bd1e4906ad2eb7205fe3abec&imgtype=0&src=http%3A%2F%2Fd.hiphotos.baidu.com%2Fexp%2Fw%3D480%2Fsign%3D7953092ec195d143da76e52b43f18296%2F8ad4b31c8701a18bbc22f762972f07082938fed6.jpg;
                    }
                    }
    
    }
    [[email protected] conf.d]# 

    访问结果如下

    原文地址:http://blog.51cto.com/jschinamobile/2163575

    时间: 2024-10-14 10:51:44

    Nginx配置静态资源缓存时间及实现防盗链的相关文章

    Nginx 配置静态文件过期时间&防盗链

    [[email protected] ~]# vim /usr/local/nginx/conf/vhosts/linux.conf location ~ .*\.(gif|jpeg|jpg|png|bmp|swf)$ { access_log off; expires 1d;  #过期时间 1天 } location ~ .*\.(js|css)  #此也也可改为  \.(js|css)来匹配 {     access_log off; expires 2h; } [[email protec

    HappyAA服务器部署笔记2(nginx的静态资源缓存配置)

    我近期对服务器进行了少量改进,虽然之前使用了nginx反向代理之后性能有所提高,但仍然不够,需要使用缓存来大幅度提高静态资源的访问速度. 服务器上的静态资源主要有这些:png, jpg, svg, js, css等.下面,我通过新的nginx配置来实现缓存.对红色的字我会额外进行说明. worker_processes 1; events { worker_connections 1024; multi_accept on; use epoll; } http { include mime.ty

    nginx配置静态文件过期时间

    1.配置指定文件不记录日志,同时设置静态文件过期时间location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${ expires 15d; access_log off;}2.只配置静态文件过期时间#配置静态文件过期时间location ~ \.(js|css)${ expires 15d;}d 天 h 小时 m 分钟 用curl 查看本地127.0.0.1访问.如:curl -x127.0.0.1:80 www.dd.com/data/cache/style_1_fo

    【记录】nginx 配置静态资源图片访问

    1:首先修改nginx的配置文件 nginx/conf/nginx.conf 2:将user nobady 改成 user root ( 原来是注释掉的) 意思是用root权限访问文件 3: nginx 配置指向地址文件 4:上传文件,我的是图片 5:重启nginx 访问地址,访问成功 参考地址:https://www.cnblogs.com/tangyin/p/9700852.html 原文地址:https://www.cnblogs.com/wbl001/p/12340984.html

    nginx配置静态资源压缩

    sendfile on; #让nginx在传输文件时直接在磁盘和tcp socket之间传输数据 location ~ .*\.(txt|xml)$ { gzip on; #开启压缩 gzip_http_version 1.1; #协议版本配置 gzip_comp_level 1; #压缩等级 gzip_types text/plain application/xml; #需要压缩的MIME类型 } 原文地址:https://www.cnblogs.com/liyuchuan/p/1071468

    Nginx配置静态资源

    打开 /etc/nginx/sites-available 的 default文件 sudo cd /etc/nginx/sites-available sudo vim default 修改default文件添加要匹配的url路径 格式: location 要匹配的路径{ root 映射到服务器文件的父路径 } laction Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: - C

    清除nginx静态资源缓存

    之前写过一篇如何配置nginx缓存及手动清除缓存的文章: http://www.cnblogs.com/Eivll0m/p/4921829.html 但如果有大量缓存需要清理,手动一条条清理就比较慢了,所以写了个小脚本进行清理,脚本如下: #!/usr/bin/env python # -*- coding: UTF-8 -*- # data:2015-12-08 # author:eivll0m # 脚本用途:清除nginx静态资源缓存 # 使用方法:将要清楚缓存的url粘贴到/app/adm

    变态的静态资源缓存与更新

    这是一个非常有趣的 非主流前端领域,这个领域要探索的是如何用工程手段解决前端开发和部署优化的综合问题,入行到现在一直在学习和实践中. 在我的印象中,facebook是这个领域的鼻祖,有兴趣.有梯子的同学可以去看看facebook的页面源代码,体会一下什么叫工程化. 接下来,我想从原理展开讲述,多图,较长,希望能有耐心看完. 让我们返璞归真,从原始的前端开发讲起.上图是一个"可爱"的index.html页面和它的样式文件a.css,用文本编辑器写代码,无需编译,本地预览,确认OK,丢到服

    .htaccess设置静态资源缓存(即浏览器缓存)

    在HTTP标头中为静态资源设置过期日期或最长存在时间,可指示浏览器从本地磁盘中加载以前下载的资源,而不是通过网络加载.这样, 网站加载速度会更快. 下面的代码都需要放到.htaccess中才能生效. 推荐设置过期时间为一个月, 即: max-age=2592000. 通过FilesMatch设置 <FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf|css|js)$">Header set Cache-Control "max-a