- nginx虛擬主機配置(利用server模塊)
基於域名的虛擬主機配置(企業級使用)
基於端口的虛擬主機配置
說明:結合OSI7層模型,熟悉虛擬主機訪問原理
基於ip地址的虛擬主機配置
說明:只要nginx配置文件中涉及ip地址信息修改,都要重新啟動 而不是平滑重啟
- nginx企業擴展應用
虛擬主信息規範配置
將多個server區塊信息,單獨放置在一個文件夾中
在nginx主配置文件中,利用include參數調用虛擬主機配置文件
配置了虛擬主機別名功能
nginx狀態模塊功能配置
stub_status on;
作用說明:主要是為了監控訪問web服務情況
- nginx相關日誌信息
日誌配置
nginx的訪問日誌配置
說明:日誌切割方法(利用腳本結合定時任務進行切割 利用logrotate服務軟件進行日誌切割)
重要:nginx必須掌握的基礎服務 MySQL數據庫 shell腳本 zabbix監控 緩存服務
Nginx服務location區塊說明
搭建測試環境
搭建好一臺nginx的web服務器。配置好內網卡地址與外網卡地址
web服務的網站域名為www.wangke.com,站點目錄為html/www
要求內網用戶可以訪問網站
http://www.wangke.com/AV.jpg資源信息(福利)
要求外網用戶禁止訪問網站www.wangke.com/AV.jpg資源信息(不讓外網看福利)
- 利用相應nginx技術手段,將訪問信息鎖定在AV.jpg上
location /AV.jpg {
allow 172.16.1.0/24;
deny all;
}
deny all :拒絕所有,全部拒絕。以上信息代表除了內網段172.16.1.0/24,其他全部拒絕訪問/AV資源
- 利用相應nginx技術手段,實現訪問策略控制
location / {
allow 172.16.1.0/24;
deny all;
}
以上信息代表除了內網段172.16.1.0/24,其他全部拒絕訪問站點目錄
測試:
location區塊官方說明
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
官方參考鏈接:
http://nginx.org/en/docs/http/ngx_http_core_module.html#location
- location區塊其實相當於 == shell if語句
“~” 用於區分大小寫(大小寫敏感)的匹配;
“~*” 用於不區分大小寫的匹配。 grep -i find -iname
“=” 表示精確匹配
”^~“ 讓匹配的優先級更高,優先匹配
作用是在進行常規的字符串匹配檢查之後,不做正則表達式的檢查,
即如果最明確的那個字符串匹配的location配置中有此前綴,那麼不做正則表達式的檢查
還可以用邏輯操作符“!”對上面的匹配取反,即“!~”和“!~*”。
- 通過實踐配置信息,瞭解location匹配符號優先級
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}
利用curl命令訪問測試:匹配優先級為
= 精確匹配,所以匹配優先級最高
^~ 第二優先級/documents/ ~* ~ 第三優先
/ 默認匹配 優先級最低
例外:取出 報錯號
curl -s -I www.etiantian.org/oldboy.html|awk 'NR==1{print $2}'
curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
curl -s -I www.etiantian.org/oldboy.html -w '%{http_code}\n' -o /dev/null
說明:通過以上命令可以實現對網站頁面的監控
nginx的rewirte作用說明
nginx 的rewite重寫模塊說明:
http://www.wangke.com/day1&study
http://www.wangke.com/day1-study
- rewrite模塊兩個功能
1. 實現網站地址信息跳轉
2. 實現偽靜態
rewrite語法格式:rewrite regex replacement [flag] == s#regex#replacement#g
rewrite應用標籤:server、location、if
官方參考鏈接:
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
練習1:
server {
listen 80;
server_name www.etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
permanent:永久的
以上配置可能會存在問題
[root@web02 conf]# curl etiantian.org
301 Moved Permanently
301 Moved Permanently
nginx/1.10.2
[root@web02 conf]# curl -L etiantian.org
curl: (47) Maximum (50) redirects followed
- 命令知識擴充:curl命令參數說明
curl -Lv
-L表示:-L/--location Follow Location: hints (H);表示追蹤訪問的過程,跟蹤Location信息;示意信息(H)
-v表示: 顯示追蹤的
- 避免無限跳轉的第一種方法
if ($host ~* "^etiantian.org$") {
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
- 避免無線跳轉的第二種方法
server { <== 添加了一個server標籤,在www.etiantian.org標籤之上
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}
flag標記符號 說明
last 本條規則匹配完後,繼續向下匹配新的location URL規律
break 本條規則匹配完即終止,不再匹配後面的任何規則
redirect 返回302臨時重定向,瀏覽器會顯示跳轉後的URL地址
permanent 返回301永久重定向,瀏覽器會顯示跳轉後的URL地址
注意:這個地方的知識點最難理解,寫的有點少,還望諒解。有時間我自己鑽研下,對於匹配哪個location模塊還是很模糊。有大神瞭解的可以給我留言下。實在沒搞明白哪個location的匹配定位到站點目錄怎麼去匹配···········
本次完,謝謝大家支持——ke.ke