Nginx location相關配置說明


  基於不同的IP、不同的端口以及不用得域名實現不同的虛擬主機,依賴於核心模塊ngx_http_core_module實現。

新建PC web站點

[root@CentOS7 ~]#mkdir /apps/nginx/conf.d
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }
}
[root@CentOS7 ~]#mkdir /data/nginx/html/pc -pv
mkdir: 已創建目錄 "/data/nginx"
mkdir: 已創建目錄 "/data/nginx/html"
mkdir: 已創建目錄 "/data/nginx/html/pc"
[root@CentOS7 ~]#echo "pc web" >>/data/nginx/html/pc/index.html
[root@CentOS7 ~]#tail -2 /apps/nginx/conf/nginx.conf
include /apps/nginx/conf.d/*.conf;
}

加載配置文件

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

訪問測試

測試機添加一條主機解析
[root@CentOS-Test ~]#cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.36.104 www.darius.com

訪問測試
[root@CentOS-Test ~]#curl www.darius.com
pc web

root與alias

  root:指定web的家目錄,在定義location的時候,文件的絕對路徑等於 root+location

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }
  location /about {
    root /data/nginx/html/pc;  # #必須要在pc目錄中創建一個about目錄纔可以訪問,否則報錯。
    index index.html;
  }
}

[root@CentOS7 ~]#mkdir /data/nginx/html/pc/about
[root@CentOS7 ~]#echo "/data/nginx/html/pc/about" >>/data/nginx/html/pc/about/index.html

重啓測試

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

訪問

[root@CentOS-Test ~]#curl -L www.darius.com/about
/data/nginx/html/pc/about

alias:定義路徑別名,會把訪問的路徑重新定義到其指定的路徑

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location /about {  # 使用alias的時候uri後面如果加了斜槓則下面的路徑配置必須加斜槓,否則403
    #root /data/nginx/html/pc;
    alias /data/nginx/html/pc;  # 當訪問about的時候,會顯示alias定義的/data/nginx/html/pc裏面的內容。
    index index.html;
  }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

訪問測試

[root@CentOS-Test ~]#curl -L www.darius.com/about
pc web

location 的使用方法

  在沒有使用正則表達式的時候,nginx會先在server中的多個location選取匹配度最高的一個uri,uri是用戶請求的字符串,即域名後面的web文件路徑,然後使用該location模塊中的正則url和字符串,如果匹配成功就結束搜索,並使用此location處理此請求。

語法規則: location [=|~|~*|^~] /uri/ { … }

= #用於標準uri前,需要請求字串與uri精確匹配,如果匹配成功就停止向下匹配並立即處理請求。
~ #用於標準uri前,表示包含正則表達式並且區分大小寫
~* #用於標準uri前,表示包含正則表達式並且不區分大寫
!~ #用於標準uri前,表示包含正則表達式並且區分大小寫不匹配
!~* #用於標準uri前,表示包含正則表達式並且不區分大小寫不匹配
^~ #用於標準uri前,表示包含正則表達式並且匹配以什麼開頭
$ #用於標準uri前,表示包含正則表達式並且匹配以什麼結尾
\ #用於標準uri前,表示包含正則表達式並且轉義字符。可以轉. * ?等
* #用於標準uri前,表示包含正則表達式並且代表任意長度的任意字符

location 精確匹配

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location = /1.jpg {
    root /data/nginx/images;
    index index.html;
  }
}
[root@CentOS7 ~]#mkdir /data/nginx/images
[root@CentOS7 ~]#rz -E
rz waiting to receive.
[root@CentOS7 ~]#mv 1.jpg /data/nginx/images/  # 上傳1.jpg到/data/nginx/images
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload  # 重啓nginx

訪問測試
Nginx location相關配置說明

location 區分大小寫

[root@CentOS7 ~]#mv /data/nginx/images/1.jpg /data/nginx/images/Darius.jpg
[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location ~ /D.*\.jpg {
    root /data/nginx/images;
    index index.html;
  }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

測試頁
Nginx location相關配置說明

Nginx location相關配置說明

location 不區分大小寫

  對用戶請求的uri做模糊匹配,也就是uri中無論都是大寫、都是小寫或者大小寫混合,此模式也都會匹配,通常使用此模式匹配用戶request中的靜態資源並繼續做下一步操作。

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  location / {
    root /data/nginx/html/pc;
  }

  location ~* /D.*\.jpg {
    root /data/nginx/images;
    index index.html;
  }
}
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload
注:不區分大小寫是對該目錄文件中的規則不區分大小寫,不是對此文件名稱不區分大小寫,在Linux中是嚴格區分文件大小寫的。

location 文件名後綴

#上傳一個和images目錄不一樣內容的的圖片1.j到/data/nginx/images1
    location ~* \.(gif|jpg|jpeg|bmp|png|tiff|tif|ico|wmf|js)$ {
        root /data/nginx/images1;
        index index.html;
    }
重啓Nginx並訪問測試

URI規則定義

[root@CentOS7-2 ~]#echo "images" >/data/nginx/images123/index.html
[root@CentOS7-2 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com_error.log;
  access_log logs/www_darius_com_access.log;
  location ^~ /images {
    root /data/nginx;
    index index.html;
  }
}

[root@CentOS7-2 ~]#nginx -s stop
[root@CentOS7-2 ~]#nginx

訪問測試(以images開頭的文件,匹配location規則)
[root@CentOS7-2 ~]#curl -L www.darius.com/images123
images

匹配案例的優先級

匹配優先級:=, ^~, ~/~*,/
location優先級:(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (/)

注:
    完整路徑就是 /static  這樣的完整的URL 。
    部分就是 使用location  ^~  /static 定義了開始,但是後面還有可能是 /statici-mage

生產中使用的案例

直接匹配網站根會加速Nginx訪問處理:
location = / {
......;
} l
ocation / {
......;
} 靜
態資源配置:
location ^~ /static/ {
......;
} #
或者
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
......;
} 多
應用配置
location ~* /app1 {
......;
} l
ocation ~* /app2 {
......;
}

Nginx 四層訪問控制

  訪問控制基於模塊ngx_http_access_module實現,可以通過匹配客戶端源IP地址進行限制。

server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com.log;
  location / {
    alias /data/nginx/html/pc;
    index index.html;
    deny 192.168.36.110;
    allow 192.168.36.0/24;
    deny all; #先允許小部分,再拒絕大部分
    }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

測試

[root@CentOS-Test ~]#curl www.darius.com   # 拒絕192.168.36.110主機
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.14.2</center>
</body>
</html>

[root@CentOS7 ~]#curl www.darius.com  # 允許192.168.36.0/24網段主機
pc web

Nginx賬戶認證功能

[root@CentOS7 ~]#yum install -y httpd-tools -y  
[root@CentOS7 ~]#htpasswd -cbm /apps/nginx/conf.d/.htpasswd user1 123456   # 生成認證用戶
Adding password for user user1
[root@CentOS7 ~]#htpasswd -bm /apps/nginx/conf.d/.htpasswd user2 123456
Adding password for user user2
[root@CentOS7 ~]#tail /apps/nginx/conf.d/.htpasswd
user1:$apr1$CsaWdjbv$EwlG9UR6hEg/RYKhP0HS/1
user2:$apr1$5/nkjgHY$Sj.vqTB6M4wqapmm.jTu3.

修改配置文件

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com.log;
  location / {
    root /data/nginx/html/pc;
    index index.html;
    auth_basic  "login password";
    auth_basic_user_file /apps/nginx/conf.d/.htpasswd;
  }
}
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload  # 重啓nginx服務

測試
Nginx location相關配置說明

自定義錯誤頁面

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_page 500 502 503 504 404 /error.html;   # 默認目錄下創建error.html頁面
  error_log logs/www_darius_com_error.log;   # 錯誤日誌
  access_log logs/www_darius_com_access.log;   # 訪問日誌
  location = /error.html {
    root html;
  }
  location / {
    root /data/nginx/html/pc;
    index index.html;
  }
}

[root@CentOS7 ~]#echo "ERROR" >/apps/nginx/html/error.html  # 創建error頁面

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

測試

[root@CentOS-Test ~]#curl www.darius.com
pc web
[root@CentOS-Test ~]#curl www.darius.com/aa  # 訪問不存在的頁面,顯示定義的錯誤頁面
ERROR

檢測文件是否存在

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

[root@CentOS7 ~]#vim /apps/nginx/conf.d/pc.conf
[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com_error.log;
  access_log logs/www_darius_com_access.log;
  location / {
    root /data/nginx/html/pc;
    index index.html;
    try_files $uri $uri/index.html $uri.html =489;   # 重啓nginx,當訪問http://www.darius.com/about/xx.html等不存在的uri顯示返回數據的狀態碼
  }
}
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

訪問測試

[root@CentOS-Test ~]#curl --head www.darius.com/about/xx.html
HTTP/1.1 489  # 489就是自定義的狀態返回碼
Server: nginx/1.14.2
Date: Thu, 30 May 2019 07:56:54 GMT
Content-Length: 0
Connection: keep-alive

當訪問的uri不存在時返回default

[root@CentOS7 ~]#cat /apps/nginx/conf.d/pc.conf
server {
  listen 80;
  server_name www.darius.com;
  error_log logs/www_darius_com_error.log;
  access_log logs/www_darius_com_access.log;
  location / {
    root /data/nginx/html/pc;
    index index.html;
    try_files $uri $uri/index.html $uri.html /about/default.html;   # 重啓nginx並測試,當訪問到http://www.darius.com/about/xx.html等不存在的uri會顯示default
  }
}

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

[root@CentOS7 ~]#echo "default" >> /data/nginx/html/pc/about/default.html

訪問測試

[root@CentOS-Test ~]#curl www.darius.com/about/xx.html
default

長連接配置

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

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

Keep-Alive:timeout=60 #瀏覽器收到的服務器返回的報文

如果設置爲0表示關閉會話保持功能,將如下顯示:
Connection:close #瀏覽器收到的服務器返回的報文

Nginx作爲下載服務器配置

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

[root@CentOS7 ~]#/apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

創建測試頁面進行測試

將光盤鏡像掛載到此目錄下進行測試
[root@CentOS7 ~]#mount /dev/sr0 /data/nginx/html/pc/download/
mount: /dev/sr0 寫保護,將以只讀方式掛載
[root@CentOS7 ~]#ll /data/nginx/html/pc/download/
總用量 1656
-rw-rw-r-- 1 root root      14 11月 26 2018 CentOS_BuildTag
drwxr-xr-x 3 root root    2048 11月 26 2018 EFI
-rw-rw-r-- 1 root root     227 8月  30 2017 EULA
-rw-rw-r-- 1 root root   18009 12月 10 2015 GPL
drwxr-xr-x 3 root root    2048 11月 26 2018 images
drwxr-xr-x 2 root root    2048 11月 26 2018 isolinux
drwxr-xr-x 2 root root    2048 11月 26 2018 LiveOS
drwxrwxr-x 2 root root 1656832 11月 25 2018 Packages
drwxrwxr-x 2 root root    4096 11月 26 2018 repodata
-rw-rw-r-- 1 root root    1690 12月 10 2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r-- 1 root root    1690 12月 10 2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root    2883 11月 26 2018 TRANS.TBL

測試頁
Nginx location相關配置說明

Nginx作爲上傳服務器

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目錄

其他配置

keepalive_disable none | browser ...;  # 對哪種瀏覽器禁用長連接

limit_except method ... { ... }  # 限制客戶端使用除了指定的請求方法之外的其它方法,僅用於location
    method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,PROPPATCH, LOCK, UNLOCK, PATCH
    示例:
        location /upload {
            root /data/magedu/pc;
            index index.html;
            limit_except GET {  # 除了GET之外的其他請求方法,僅允許192.168.36.110主機使用
            allow 192.168.36.110;
            deny all;
            }
        }
aio on | off #是否啓用asynchronous file I/O(AIO)功能,需要編譯開啓
linux 2.6以上內核提供以下幾個系統調用來支持aio:
1、SYS_io_setup:建立aio 的context
2、SYS_io_submit: 提交I/O操作請求
3、SYS_io_getevents:獲取已完成的I/O事件
4、SYS_io_cancel:取消I/O操作請求
5、SYS_io_destroy:毀銷aio的context
directio size | off; #操作完全和aio相反,aio是讀取文件而directio是寫文件到磁盤,啓用直接I/O,默認爲關閉,當文件大於等於給定大小時,例如directio 4m,同步(直接)寫磁盤,而非寫緩存。
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;  # 緩存錯誤信息
server_tokens off;  # 隱藏Nginx server版本

[root@CentOS7 ~]#grep "server_tokens" /apps/nginx/conf/nginx.conf
    server_tokens off;
[root@CentOS7 ~]#/apps/nginx/sbin/nginx -s reload

訪問測試
[root@CentOS-Test ~]#curl --head www.darius.com
HTTP/1.1 200 OK
Server: nginx       # 版本號已隱藏
Date: Thu, 30 May 2019 08:27:49 GMT
Content-Type: text/html
Content-Length: 7
Last-Modified: Thu, 30 May 2019 03:06:03 GMT
Connection: keep-alive
ETag: "5cef489b-7"
Accept-Ranges: bytes
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章