nginx 做爲靜態資源web服務

Nginx做爲靜態資源web服務

靜態資源web服務-CDN場景javascript

Nginx資源存儲中心會把靜態資源分發給「北京Nginx」,「湖南Nginx」,「山東Nginx」。php

而後北京User發送靜態資源請求,經過CDN,找到離本身最近的「北京Nginx」。css

靜態資源核心配置

文件讀取 sendfilejava

sendfile 是一種高效傳輸文件的模式.
sendfile設置爲on表示啓動高效傳輸文件的模式。sendfile能夠讓Nginx在傳輸文件時直接在磁盤和tcp socket之間傳輸數據。若是這個參數不開啓,會先在用戶空間(Nginx進程空間)申請一個buffer,用read函數把數據從磁盤讀到cache,再從cache讀取到用戶空間的buffer,再用write函數把數據從用戶空間的buffer寫入到內核的buffer,最後到tcp socket。開啓這個參數後能夠讓數據不用通過用戶buffer。

sendfile語法node

  • Syntax:sendfile on | off;
  • Default:sendfile off;
  • Context:httpserverlocationif in location

 

語法解釋:nginx

Enables or disables the use of sendfile().web

Starting from nginx 0.8.12 and FreeBSD 5.2.1, aio can be used to pre-load data for sendfile():bash

配置語法-tcp_nopush服務器

做用:網絡

  •  sendfile開啓的狀況下,提升網絡包的傳輸效率。

tcp_nopush語法

  • Syntax: tcp_nopush on | off;
  • Default:tcp_nopush off;
  • Context:http, server, location

語法解釋

Enables or disables the use of the TCP_NOPUSH socket option on FreeBSD or the TCP_CORK socket option on Linux. The options are enabled only when sendfile is used. Enabling the option allows

sending the response header and the beginning of a file in one packet, on Linux and FreeBSD 4.*;

sending a file in full packets.

在 sendfile 開啓的狀況下,提升網絡數據包的傳輸效率。
tcp_nopush指令,在鏈接套接字時啓用Linux系統下的TCP_CORK。該選項告訴TCP堆棧附加數據包,並在它們已滿或當應用程序經過顯式刪除TCP_CORK指示發送數據包時發送它們。 這使得發送的數據分組是最優量,而且所以提升了網絡數據包的傳輸效率。
也就是說 tcp_nopush=on 時,結果就是數據包不會立刻傳送出去,等到數據包最大時,一次性的傳輸出去,這樣有助於解決網絡堵塞,雖然有一點點延遲。
 
配置語法
  • Syntax: tcp_nopush on | off;
  • Default: tcp_nopush off;
  • Context: http, server, location

 

配置語法-tcp_nodelay

 做用:

  • keepalive鏈接下,提升網絡包的傳輸實時性。

tcp_nodelay語法

  • Syntax: tcp_nodelay on | off;
  • Default: tcp_nodelay on;
  • Context: http, server, location

在 keepalive 鏈接下,提升網絡數據包的傳輸實時性。
tcp_nodelay選項和tcp_nopush正好相反,數據包不等待,實時發送給用戶。

配置語法-gzip壓縮

做用:

  • 壓縮傳輸,提升傳輸效率。
  • 開啓壓縮,能夠加快資源響應速度,同時節省網絡帶寬資源。

 

gzip語法

  • Syntax: gzip on | off;
  • Default: gzip off; Context:
  • http, server, location, if in location

 配置壓縮比,壓縮等級配置(壓縮等級越高,越消耗服務器資源)

  • Syntax: gzip_comp_level level;
  • Default: gzip_comp_level 1;
  • Context: http, server, location

 gzip協議版本配置

  • Syntax: gzip_http_version 1.0 | 1.1;
  • Default: gzip_http_version 1.1;
  • Context: http, server, location

 壓縮擴展模塊,預讀gzip功能 ngx_http_gzip_static_module

  • Syntax: gzip_static on | off | always;
  • Default: gzip_static off;
  • Context: http, server, location

 應用支持gunzip的壓縮方式 ngx_http_gunzip_module

  • Syntax: gunzip on | off;
  • Default: gunzip off;
  • Context: http, server, location
  • Syntax: gunzip_buffers number size;
  • Default: gunzip_buffers 32 4k|16 8k;
  • Context: http, server, location

配置語法-gzip_static

做用:

 傳輸預壓縮靜態文件給客戶端(.gz文件爲預壓縮)

gzip_static語法

  • Syntax: gzip_static on | off | always;
  • Default: gzip_static off;
  • Context: http, server, location

語法解釋:

Enables (「on」) or disables (「off」) checking the existence of precompressed files. The following directives are also taken into account: gzip_http_version, gzip_proxied, gzip_disable, and gzip_vary.

With the 「always」 value (1.3.6), gzipped file is used in all cases, without checking if the client supports it. It is useful if there are no uncompressed files on the disk anyway or the ngx_http_gunzip_module is used.

The files can be compressed using the gzip command, or any other compatible one. It is recommended that the modification date and time of original and compressed files be the same.

 

 gzip 壓縮圖片 小案例

   sendfile on;
  location ~ .*\.(jpg|gif|png)$ {
        #gzip on;
        #gzip_http_version 1.1;
        #gzip_comp_level 2;
        #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/images;
    }

 

訪問

http://192.168.1.112/wei.png

能夠看到圖片的大小是 239KB

 

    sendfile on;
    location ~ .*\.(jpg|gif|png)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/images;
    }

參數解讀:

  • gzip on – nginx是否開啓gzip壓縮功能;
  • gzip_min_length 1000 – nginx限制最小gzip壓縮資源大小;
  • gzip_proxied – nginx做爲反向代理時,容許gzip壓縮請求或響應頭裏字段;
  • gzip_types – nginx容許gzip壓縮的靜態資源類型;
  • gzip_http_version 1.1 – nginx容許gzip壓縮的HTTP請求版本;
  • gzip_comp_level – nginx容許壓縮的級別,共9個等級,級別越高壓縮率越大;

再次刷新頁面

打開 giz 的功能大小是 182 B

 

 gzip 壓縮文本 小案例

   sendfile on;
  location ~ .*\.(txt|xml)$ {
        #gzip on;
        #gzip_http_version 1.1;
        #gzip_comp_level 1;
        #gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/doc;
    }

訪問

http://192.168.1.112/access.txt

 

能夠看到沒開啓 gzip 的大小是 175 kb

 

開啓 gizp 功能

    sendfile on;
    location ~ .*\.(txt|xml)$ {
        gzip on;
        gzip_http_version 1.1;
        gzip_comp_level 1;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
        root  /opt/app/code/doc;
    }

 

再次刷新頁面

 能夠看到大小爲 182 B

gzip 文件預讀案例演示

開啓gzip 預讀功能

    sendfile on;
    location ~ ^/download {
        gzip_static on;
        tcp_nopush on;
        root /opt/app/code;
    }

參數解讀:

  • gzip_static on – nginx是否開啓預讀gzip文件功能;
  • tcp_nopush on – nginx是否一次性發送整個文件,提升傳輸效率;
  • root /opt/app/code  –  指定根目錄;

注意:代碼的目錄是 /opt/app/code

訪問/download/test.img 的時候會在 /opt/app/code/download/ 下面去找 test.img.zp 或test.img, 出錯能夠經過查看錯誤日誌

 

pwd
ls

 

 關閉 gzip_static on 後訪問出錯

相關文章
相關標籤/搜索