配置Nginx网页缓存时间!

当 Nginx 将网页数据返回给客户端后,可设置缓存的时间,以方便在日后进行相同内容
的请求时直接返回,以避免重复请求,加快了访问速度,一般针对静态网页进行设置,对动
态网页不用设置缓存时间。可在 Windows 客户端中使用 fiddler 查看网页缓存时间。
设置方法:
在 可修改配置文件,在 http 段、或 server 段、或者 location 段加入对特定内容的过期参
数。

====================================================================

[root@localhost html]# vim /etc/nginx.conf 

复制代码

user nginx nginx;
worker_processes 2;


error_log logs/error.log;
error_log logs/error.log info;


pid logs/nginx.pid;



events {
       use epoll;
       worker_connections 10240;
}

http {
      include mime.types;
      default_type application/octet-stream;


       log_format main '$remote_addr - $remote_user [$time_local] "$request" '
       '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';


          access_log logs/access.log main;
         sendfile on;
         server_tokens off;
         keepalive_timeout 65;


server {
        listen 80;

        server_name localhost;

         charset utf-8;


location / {
         root html;
         index index.html index.htm;
}
location ~* \.(gif|jpg|jpeg|bmp|ico)$ {               //如果是这种类型的文件缓存时间为一天
        expires 1d;
}
location ~* \.(js|css)$ {                            //如果是js或css类型的文件缓存为一小时
       expires 1h;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
     }

   }

}

复制代码

[root@localhost nginx-1.16.0]# cd
[root@localhost ~]# cd /usr/local/nginx/html/

导入图片
[root@localhost html]# rz
z waiting to receive.**B0100000023be50
[root@localhost html]# ls
50x.html index.html linux.jpg

[root@localhost html]# vim index.html

在尾部添加如下代码:

<p><em>Thank you for using nginx.</em></p>
<img src='linux.jpg'/>
</body>
</html>
~                 

[root@localhost html]# killall -s HUP nginx                           //重启配置

 

 

 

posted @ 2019-09-14 10:41  奥义,三千世界  阅读(2989)  评论(0编辑  收藏  举报