您当前的位置: 首页 >  缓存

小志的博客

暂无认证

  • 1浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Nginx——nginx作为静态资源web服务(浏览器缓存示例演示)

小志的博客 发布时间:2021-07-29 23:08:04 ,浏览量:1

目录
    • 一、添加浏览器缓存配置语法(即添加Cache-Control、Expires头信息)
    • 二、添加浏览器缓存的配置演示
      • 1、配置nginx访问html页面的路径
      • 2、编辑nginx.conf配置文件
      • 3、启动nginx服务并检查配置文件
      • 4、浏览器第一次请求testExpires.html页面
      • 5、浏览器第二次请求testExpires.html页面

一、添加浏览器缓存配置语法(即添加Cache-Control、Expires头信息)
  • Syntax:expires [modified] time; 表示缓存的有效期;
  • Default:expires off; 表示默认关闭
  • Context:http,server,location; 表示需要在http块、或server块、或location块中
二、添加浏览器缓存的配置演示 1、配置nginx访问html页面的路径
  • 创建testExpires.html页面,上传到/opt/app/html目录下
    
    	
    		
    		expires
    	
    	
    		hello world!
    	
    
    
2、编辑nginx.conf配置文件
  • 编辑nginx.conf配置文件,在http块的server块的location块中配置访问/opt/app/html目录下的testExpires.html页面,如下内容

    location ~ .*\.(html|htm)$ {
    expires 24h;
    root /opt/app/html;
    }
    

    在这里插入图片描述

3、启动nginx服务并检查配置文件
  • 启动nginx服务

    [root@localhost nginx]# systemctl start nginx.service
    

    在这里插入图片描述

  • 检查配置修改的配置文件是否正确,返回successful表示配置文件修改无错

    [root@localhost nginx]# nginx -t -c /etc/nginx/nginx.conf
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    在这里插入图片描述

  • 重新加载配置文件

    [root@localhost /]# systemctl reload nginx
    

    在这里插入图片描述

4、浏览器第一次请求testExpires.html页面
  • 浏览器第一次请求的状态、请求头信息,服务端返回的头信息如下图:

    在这里插入图片描述在这里插入图片描述在这里插入图片描述

5、浏览器第二次请求testExpires.html页面
  • 浏览器第二次请求的状态、请求头信息,服务端返回的头信息如下图:

    在这里插入图片描述在这里插入图片描述

关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.1574s