Nginx動靜分離

來源:互聯網
上載者:User

標籤:processes   分享   變化   inf   cat   oct   首頁   events   div   

當我們請求一個網頁的時候,可能會載入很多css,js,img等靜態檔案;一般這些檔案是很久都不會變化的,所以我們為了提高頁面響應速度,完全可以將這些檔案快取到瀏覽器中(可以理解為cookie資訊),這就叫動靜分離。

1.  vim nginx.conf

worker_processes  1;events {    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;        log_format  wuleiformat  ‘$remote_addr - $remote_user [$time_local]‘;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  localhost;        # 輸入ip預設訪問html檔案夾下的index.html檔案        location / {            root html;            index index.html index.htm;        }        # 所有.png  .css ...結尾的檔案都直接去/html/images裡面找        location ~ \.(png|jpg|js|css|gif)$ {          root html/images;          expires 10m;  # 緩衝10分鐘           # s秒   m分   h時   d天        }    }}

2.  修改首頁index.html  讓它去載入一個靜態圖片。

 3.  重啟可以看到效果(圖片第一次載入花了7ms,後面再次重新整理就是0ms,查看詳細資料看到34分第一次緩衝的,直到44分失效)。

 

Nginx動靜分離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.