文章目錄
- 一、localtion配置
- 1.匹配規則
- 2.匹配優先級
- 3.配置案例
- 二、rewrite
- 1、 語法
- 2、 可寫入字段
- 3 配置案例
- 4 if 指令
- 5.sutoindex
- 6. nginx配置中的常用變量
- 三、配置Nginx狀態統計
- 1.下載vts模塊
- 2.編譯nginx
提示:以下是本篇文章正文內容,下面案例可供參考
一、localtion配置
配置路徑
location [ = | ~ | ~* | ^~ ] uri {
…
}
##在瀏覽器中輸入的:http://www.jx.com/s?id=1&test=123,稱作URL,即:統一資源定位符
##在WEB服務器中,對資源進行標識,URI,即統一資源標識符
寫在server字段中。
1.匹配規則
- **=** 精確匹配;
- **~** 正則匹配,區分大小寫;
- **~\*** 正則匹配,不區分大小寫;
- **^~** 匹配到即停止搜索;
2.匹配優先級
= > ^~ > ~ > ~* > 不帶任何字符 “/”。
3.配置案例
注:
1.
匹配的文件或者目錄
location /test {
…
}
只匹配目錄
location /test/ {
…
}
2.return ::停止處理請求,直接返回響應碼或重定向到其他 URL ;執行 return 指令后, location 中后續指令將不會被執行
return code [text];
return code URL;
return URL;例如:
location / {return 404; # 直接返回狀態碼
}location / {return 404 "pages not found"; # 返回狀態碼 + 一段文本
}#location / {
# return 302 /bbs ; # 返回狀態碼 + 重定向地址
#}location / {return https://www.baidu.com ; # 返回重定向地址
}
二、rewrite
根據指定正則表達式匹配規則,重寫 URL 。應用場景: 新老域名的更替!!!
1、 語法
rewrite 正則表達式 要替換的內容 [flag];
2、 可寫入字段
server、location、if
示例:
rewirte /images/(.*\.jpg)$ /pic/$1; # $1是前面括號(.*\.jpg)的反向引用
flag 可選值的含義:
- last: 重寫后的 URL 發起新請求,再次進入 server 段,重試 location 的中的匹配;
- break :直接使用重寫后的 URL ,不再匹配其它 location 中語句;
- redirect :返回 302 臨時重定向;
- permanent :返回 301 永久重定向。
3 配置案例
server{listen 80;server_name www.jx.com; # 要在本地hosts文件進行配置root /usr/share/nginx/html;location /search {rewrite ^/(.*) https://www.baidu.com redirect;}location /images {rewrite /images/(.*) /pics/$1;}location /pics {rewrite /pics/(.*) /photos/$1;}location /photos {}
}
###生產環境不能使用.*,表示匹配任意!!!
#解析:
#當訪問 www.jx.com/search 時,會自動幫我們重定向到 https://www.baidu.com;
#當訪問 www.jx.com/images/1.jpg 時,第一步重寫 URL 為 www.jx.com/pics/1.jpg ,找到 pics 的 location ,繼續重寫 URL 為 www.jx.com/photos/1.jpg ,找到 /photos 的 location 后,去 html/photos 目錄下尋找 1.jpg 靜態資源。
4 if 指令
**語法:**if (condition) {…}
**可寫字段:**server、location
示例:
location / {if ($http_user_agent ~ Chrome) {rewrite /(.*) /Chrome/$1 break;}if ($http_user_agent ~ Firefox) {rewrite /(.*) /Firefox/$1 break;}}
##測試192.168.115.111/index.html
set $var value; #設置變量
condition 判斷條件:
- $variable 僅為變量時,值為空或以0開頭字符串都會被當做 false 處理;
- = 或 != 相等或不等;
- ~ 正則匹配;
- ! ~ 非正則匹配;
- ~* 正則匹配,不區分大小寫;
- -f 或 ! -f 檢測文件存在或不存在;
- -d 或 ! -d 檢測目錄存在或不存在;
- -e 或 ! -e 檢測文件、目錄、符號鏈接等存在或不存在;
- -x 或 ! -x 檢測文件可以執行或不可執行;
配置實例
server {listen 8080;server_name www.jx.com;root /usr/share/nginx/html;location / {if ( $uri = "/images/" ){rewrite (.*) /pics/ break;}}
}
#當訪問 www.jx.com/images/ 時,會進入 if 判斷里面執行 rewrite 命令。
5.sutoindex
用戶請求以 / 結尾時,列出目錄結構,可以用于快速搭建靜態資源下載網站。
這里是引用`在這里插入代碼片
server {
listen 80;
server_name www.jx.com;
location /download/ {
root /usr/share/nginx/html;
autoindex on; # 打開 autoindex,,可選參數有 on | off
autoindex_exact_size on; # 修改為off,以KB、MB、GB顯示文件大小,默認為on,以bytes顯示出?件的確切??
autoindex_format html; # 以html的方式進行格式化,可選參數有 html | json | xml
autoindex_localtime off; # 顯示的?件時間為?件的服務器時間。默認為off,顯示的?件時間為GMT時間
}
}
#當訪問 www.jx.com/download/ 時,會把服務器 /usr/share/nginx/html/download/ 路徑下的文件展示出來.
`
6. nginx配置中的常用變量
變量名 | 含義 |
---|---|
remote_add | 客戶端IP地址 |
remote_port | 客戶端端口 |
server_addr | 服務端IP地址 |
Server_port | 服務端端口 |
server_protocol | 服務端協議 |
binary_remote_addr | 二進制格式的客戶端IP地址 |
connection | TCP連接的序號,遞增 |
connection_request | TCP連接當前的請求數量 |
uri | 請求的URL,不包含參數 |
request ur | 請求的URL,包含參數 |
scheme | 協議名,http或https |
request metho | 請求方法 |
request_length | 全部請求的長度,包含請求行、請求頭、請求體 |
args | 全部參數字符串 |
arg_參數名 | 獲取特定參數值 |
is_args | URL中是否有參數,有的話返回?,否則返回空 |
query_string | 與args相同 |
host | 請求信息中的Host,如果請求中沒有Host行,則在請求頭中找,最后 使用nginx中設置的server_name。 |
http_user_agent | 用戶訪問方式 |
http_referer | 從哪些鏈接過來的請求 |
http_via | 每經過一層代理服務器,都會添加相應的信息 |
http_cookie | 獲取用戶cookie |
request time | 處理請求已消耗的時間 |
https | 是否開啟了https,是則返回on,否則返回空 |
request_filename | 磁盤文件系統待訪問文件的完整路徑 |
document_root | 由URI和root/alias規則生成的文件夾路徑 |
limit_rate | 返回響應時的速度上限值 |
三、配置Nginx狀態統計
1.下載vts模塊
https://github.com/vozlt/nginx-module-vts
2.編譯nginx
tar xf nginx-module-vts-master.zip
cd nginx-1.22.1/
./configure --prefix=/usr/local/nginx/ --add-module=/root/nginx-module-vts-master && make && make install
注:
如果之前是編譯安裝,在安裝新的模塊前帶上之前編譯的所有模塊,然后再make&&make install
把原來的備份一下
mv /usr/local/nginx/sbin/nginx /opt/nginx.bak
新的移動到我們定義的目錄下
cp ./nginx /usr/local/nginx/sbin
在檢查下版本就有這個新的模塊了
在去 /usr/local/nginx//conf/nginx.conf下添加如下內容
訪問結果