配置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                           //重啟配置

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM