Nginx-檢測文件是否存在(try_files),長連接配置,上傳與下載服務器,限制位置內允許的 HTTP 方法,AIO與sendfile,緩存


檢測文件是否存在

try_files會按順序檢查文件是否存在,返回第一個找到的文件或文件夾(結尾加斜線表示為文件夾),如果所有文件或文件夾都找不到,會進行一個內部重定向到最后一個參數。只有最后一個參數可以引起一個內部重定向,之前的參數只設置內部URI的指向。最后一個參數是回退URI且必須存在,否則會出現內部500錯誤。

官網文檔:http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

location /about {
  root /data/nginx/html/pc;
  #alias /data/nginx/html/pc;
  index index.html;
  #try_files $uri /about/default.html;
  try_files $uri $uri/index.html $uri.html /default.html;  //將用戶訪問的uri最后一個參數名稱作為新的變量名稱匹配服務器內部的資源,依次匹配,如果匹配成功則返回,如果未匹配成功,則返回default.html頁面。
  #try_files $uri $uri/index.html $uri.html =489; //如果前面都沒有匹配成功,則返回自定義錯誤碼 }
]# echo "default" >> /data/nginx/html/pc/about/default.html
]# mkdir  /data/nginx/html/pc/about/index1
]# echo "index v2" > /data/nginx/html/pc/about/index1/index.html
]# echo "index v1" > /data/nginx/html/pc/about/index1.html
 重啟nginx並測試
conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
conf.d]# nginx -s reload
about]# curl http://www.magedu.net/about/index.html about index about]# curl http://www.magedu.net/about/index1.html index1 v1 about]# rm -rf index1.html //刪除about路徑下的文件,使$uri.html不能匹配,測試$uri/功能 about]# curl http://www.magedu.net/about/index1.html //會將index.html作為文件夾名稱匹配about路徑下的/abuot/index.html/index.html,可以看到前面未匹配成功,匹配到了default。 default
about]# curl http://www.magedu.net/about/index1 //$uri/ 將index1作為文件夾名稱匹配到了about/index1文件 index1 v2

修改配置文件
location /about {
          root /data/nginx/html/pc;
          index index.html;
          #try_files $uri /about/default.html;
          try_files $uri $uri/index1.html $uri.html /default.html;   //將$uri/ 默認訪問文件改為index1.html
          #try_files $uri $uri/index.html $uri.html =489;
        }
重新加載訪問
about]# curl http://www.magedu.net/about/index1
default

[root@nginx-master about]# ll index1/
total 4
-rw-r--r--. 1 nginx nginx 11 Jan  9 00:13 index.html
[root@nginx-master about]# echo "index1 v3" > index1/index1.html
[root@nginx-master about]# curl http://www.magedu.net/about/index1   //可以看到配置文件中$uri/ 后面指定訪問的文件名稱后,這個文件必須存在,不然匹配不到。
index1 v3
如果是自定義的狀態碼則會顯示在返回數據的狀態碼中
[root@s2 about]# curl --head http://www.magedu.net/about/xx.html 
HTTP/1.1 489 #489就是自定義的狀態返回碼 
Server: nginx 
Date: Thu, 21 Feb 2019 00:11:40 GMT 
Content-Length: 0 
Connection: keep-alive 
Keep-Alive: timeout=65

 長連接

keepalive_timeout number; #設定保持連接超時時長,0表示禁止長連接,默認為75s,通常配置在http字段作為站點全局配置 keepalive_requests number; #在一次長連接上所允許請求的資源的最大數量,默認為100次

也可以設置在location,server
http { keepalive_timeout
65 60; //65為一次客戶端和服務器長連接超時時間,60為瀏覽器端顯示時間 keepalive_requests 200; //200為一次TCP連接客戶端與服務器可以交互的請求次數 ... }

開啟長連接后,返回客戶端的會話保持時間為60s,單次長連接累計請求達到指定次數請求或65秒就會被斷開,后面的60為發送給客戶端應答報文頭部中顯示的超時時間設置為60s:如不設置客戶端將不顯示超時時間。

作為下載服務器配置

[root@s2 about]# mkdir /data/nginx/html/pc/download
#download不需要index.html文件
[root@s2 about]# vim
/apps/nginx/conf/conf.d/pc.conf location /download {

    auth_basic "Restricted";

    auth_basic_user_file /usr/local/nginx/conf/passwords;

  autoindex off;   #關閉自動索引功能
  autoindex_exact_size  on; #計算文件確切大小(單位bytes),off只顯示大概大小(單位kb、mb、gb)
autoindex_localtime  on; #顯示本機時間而非GMT(格林威治)時間  
root /data/nginx/html/pc; }
limit_rate 10k; #限制響應給客戶端的傳輸速率,單位是bytes/second,默認值0表示無限制

生成用戶密碼
htpasswd -c passwords user
[root@s2 pc]# cp /root/anaconda-ks.cfg /data/nginx/html/pc/download/

重啟Nginx並訪問測試下載頁面

 限速與不限速的對比:

 

 作為上傳服務器

配置路徑:location,server,http

client_max_body_size 1m; #設置允許客戶端上傳單個文件的最大值,默認值為1m client_body_buffer_size size; #用於接收每個客戶端請求報文的body部分的緩沖區大小;默認16k;超出
此大小時,其將被暫存到磁盤上的由下面client_body_temp_path指令所定義的位置
client_body_temp_path path [level1 [level2 [level3]]]; #設定存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量,目錄名為16進制的數字,使用hash之后的 值從后往前截取1位、2位、2位作為文件名:

[root@s3 ~]# md5sum /data/nginx/html/pc/index.html

  95f6f65f498c74938064851b1bb 96 3d 4 /data/nginx/html/pc/index.html
  1級目錄占1位16進制,即2^4=16個目錄  0-f
  2級目錄占2位16進制,即2^8=256個目錄 00-ff
  3級目錄占2位16進制,即2^8=256個目錄 00-ff
  

  配置示例:
   client_max_body_size 10m;
  client_body_buffer_size 16k;
  client_body_temp_path  /apps/nginx/temp 1 2 2; #reload Nginx會自動創建temp目錄,注意目錄權限

對哪種瀏覽器禁用長連接  

Default:    
keepalive_disable msie6;  //IE6瀏覽器不支持長連接
Context: http, server, location

限制位置內允許的 HTTP 方法

Syntax:    limit_except method ... { ... }
Default:    —
Context:    location

限制位置內允許的 HTTP 方法。method參數可以是下列之一: GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK,或 PATCH允許該GET方法使得該 HEAD方法也被允許。可以使用ngx_http_access_module、 ngx_http_auth_basic_module和 ngx_http_auth_jwt_module (1.13.10) 模塊指令來限制對其他方法的訪問 

limit_except GET {
    allow 192.168.1.0/32;
    deny  all;
}

請注意,這將限制對GET 和 HEAD之外的所有方法的訪問 

 測試訪問

]# curl -XPUT /etc/issue http://www.magedu.net/upload
curl: (3) <url> malformed
<html>
<head><title>403 Forbidden</title></head> #Nginx拒絕上傳
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx</center>
</body>
</html>

 AIO與sendfile

需要編譯時加載模塊 --with-file-aio

Syntax:    aio on | off | threads[=pool];
Default:    
aio off;
Context:    http, server, location
This directive appeared in version 0.8.11.

在 Linux 上,可以從內核版本 2.6.22 開始使用 AIO。此外,必須啟用 directio,否則讀取將被阻塞:

location /video/ {
    aio            on;
    directio       512;
    output_buffers 1 128k;
}

在 Linux 上同時啟用 AIO 和sendfile 時,AIO 用於大於或等於directio指令中指定大小的文件,而sendfile用於較小大小的文件或禁用directio 

location /video/ {
    sendfile       on;
    aio            on;
    directio       8m;   #directio是直接讀寫文件到磁盤,啟用直接I/O,默認為關閉,當文件大於等於給定大小時,例如directio 4m,可以和sebdfile結合使用,比如當大於此值使用AIO,當小於此值使用sendfile
}

 緩存

Context:    http, server, location
open_file_cache off;  #是否緩存打開過的文件信息
open_file_cache max=N [inactive=time];
nginx可以緩存以下三種信息: (
1) 文件元數據:文件的描述符、文件大小和最近一次的修改時間 (2) 打開的目錄結構 (3) 沒有找到的或者沒有權限訪問的文件的相關信息 max=N:可緩存的緩存項上限數量;達到上限后會使用LRU(Least recently used,最近最少使用)算法實現管理 inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少於open_file_cache_min_uses指令所指定的次數的緩存項即為非活動項,將被刪除
open_file_cache_errors on
| off; 是否緩存查找時發生錯誤的文件一類的信息 默認值為off
open_file_cache_min_uses number; open_file_cache指令的inactive參數指定的時長內,至少被命中此處指定的次數方可被歸類為活動項 默認值為1
open_file_cache_valid
time; 緩存項有效性的檢查驗證頻率,默認值為60s

例子: open_file_cache max
=10000 inactive=60s; #最大緩存10000個文件,非活動數據超時時長60s open_file_cache_valid  60s;  #每間隔60s檢查一下緩存數據有效性 open_file_cache_min_uses 5; #60秒內至少被命中訪問5次才被標記為活動數據 open_file_cache_errors  on; #緩存錯誤信息

 


免責聲明!

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



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