Nginx基礎(chǔ)應(yīng)用
1.Nginx目錄索引
-
目錄索引模塊簡述
ngx_http_autoindex_module 模塊處理以斜杠字符 ('/') 結(jié)尾的請求,并生成目錄列表。
當 ngx_http_index_module 模塊找不到索引文件時,通常會將請求傳遞給ngx_http_autoindex_module 模塊。
-
配置
Nginx 默認是不允許列出整個目錄瀏覽下載。
Syntax: autoindex on | off; Default: autoindex off; Context: http, server, location ? # autoindex常用參數(shù) autoindex_exact_size off; 默認為on, 顯示出文件的確切大小,單位是bytes。 修改為off,顯示出文件的大概大小,單位是kB或者MB或者GB。 ? autoindex_localtime on; 默認為off,顯示的文件時間為GMT時間。 修改為on, 顯示的文件時間為文件的服務(wù)器時間。 ? charset utf-8,gbk; 默認中文目錄亂碼,添加上解決亂碼。 ?
-
配置站點目錄瀏覽功能
//開啟目錄瀏覽 location / { root html; autoindex on; autoindex_localtime on; autoindex_exact_size off; charset utf-8,gbk; }
案例
自定義一個站點,要求如下: 1)通過域名www.zmj.com來訪問 server_name www.zmj.com; 2)文檔根目錄在/web/www目錄 root /web/www; 3)默認索引頁index.html index index.html; 4)有獨立的日志文件 access_log logs/nrj_access.log; error_log logs/nrj_error.log;
創(chuàng)建日志文件
[root@web01 conf.d]# mkdir -p /usr/share/nginx/logs [root@web01 conf.d]# mkdir -p /www/
創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/111.conf
server{ ? ? ? listen 80; ? ? ? server_name www.zmj.com; ? ? ? access_log logs/zmj_access.log; ? ? ? error_log logs/zmj_error.log; ? ? ? index index.html; ? ? ? location /{ ? ? ? ? ? ? ? root /web/www; ? ? ? } ? ? ? ?#如果需要監(jiān)控 ? ? ? ?#location /status{ ? ? ? #stub_status; ? ? ? #access_log off; ? ? ? ?#} }
重啟nginx服務(wù)
systemctl reload nginx
修改本機hosts文件
C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 www.zmj.com
案例 將web文件夾進行目錄索引
創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/111.conf
www.download.com server{ listen 80; server_name download1zmj.com; index index.html; charset ntf-8,gbk; location /{ root /web; autoindex on; autoindex_exact_size off; autoindex_localtime on; } }
重啟nginx服務(wù)
systemctl reload nginx
修改本機hosts文件
C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 downloadzmj.com
Nginx狀態(tài)監(jiān)控
-
ngx_http_stub_status_module 用于展示 Nginx 連接狀態(tài)信息, 需要 --with-http_stub_status_module 模塊支持
Syntax: stub_status; Default: — Context: server, location
2.配置Nginx status
location /nginx_status { stub_status; access_log off; }
3.使用瀏覽器訪問 http://IP/nginx_status 訪問后得到的結(jié)果
Active connections: 2 server accepts handled requests 4 4 61 Reading: 0 Writing: 1 Waiting: 1 Active connections # 當前活動的TCP連接數(shù) accepts 4 # 當前的TCP總連接數(shù) handled 4 # 成功的TCP連接數(shù) requests 61 # 總的http請求數(shù) Reading # 請求 Writing # 響應(yīng) Waiting # 等待的請求數(shù),開啟了keepalive # 注意, 一次TCP的連接,可以發(fā)起多次http的請求, 如下配置參數(shù)可驗證 keepalive_timeout 0; # 類似于關(guān)閉長連接 keepalive_timeout 65; # 65s沒有活動則斷開連接
案例
訪問控制,允許10.0.0.0內(nèi)網(wǎng)段的所有IP訪問,拒絕其他所有 (并監(jiān)控)
創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/111.conf
server{ listen 80; server_name www.ccc.com; access_log logs/ccc_access.log; error_log logs/ccc_error.log; index index.html; location /{ root /web/aaa; stub_status; ? ? #監(jiān)控 access_log off; ?#關(guān)閉 日志配置 allow 10.0.0.0/24; deny all; } }
重啟nginx服務(wù)
systemctl reload nginx
修改本機hosts文件
C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 www.zmj.com
Active connections: 1 表示nginx活躍的連接數(shù) server accepts handled requests 5 5 5 第一個整數(shù)表示nginx處理握手的總次數(shù);第二個整數(shù)表示nginx處理的連接數(shù),第三個整數(shù)表示nginx處理的總的請求數(shù), Reading: 0 表示連接狀態(tài),0表示nginx讀的數(shù)量 Writing: 1 表示連接狀態(tài),1表示nginx寫的數(shù)量 Waiting: 0 表示連接狀態(tài),0表示nginx開啟了常連接的情況下,客戶端和服務(wù)端正在空閑的等待,既沒有讀也沒有寫,建立連接的數(shù)量
Nginx訪問控制
基于IP的訪問控制 http_access_module
基于用戶登陸認證 http_auth_basic_module
1.Nginx基于IP的訪問控制
//允許配置語法 Syntax: allow address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except //拒絕配置語法 Syntax: deny address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except
1).訪問控制配置示例, 拒絕指定的IP, 其他全部允許
location /nginx_status { stub_status; access_log off; deny 10.0.0.1; allow all; }
2).訪問控制配置示例, 只允許誰能訪問, 其它全部拒絕
location / { root html; index index.html index.htm; allow 10.0.0.0/24; allow 127.0.0.1; deny all; }
2.Nginx 基于用戶登陸認證
//配置語法 Syntax: auth_basic string| off; Default: auth_basic off; Context: http, server, location, limit_except //用戶密碼記錄配置文件 Syntax: auth_basic_user_file file; Default: - Context: http, server, location, limit_except //需要安裝依賴組件 [root@web01 ~]# yum install httpd-tools [root@web01 ~]# htpasswd -b -c /etc/nginx/auth_conf tf 123 //可在http,server,location下添加如下信息 auth_basic "access auth,input your password!"; auth_basic_user_file /etc/nginx/auth_conf;
案例:只能10.0.0.1和本機可以訪問
創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/zmj.conf
server{ listen 80; server_name www.fangwen.com; access_log logs/fangwen_access.log; error_log logs/fangwen_error.log; index index.html; location / { root /web/www; } location /status { stub_status; access_log off; allow 10.0.0.1; allow 10.0.0.7; deny all; } }
檢查語法是否正確
nginx -t
查看錯誤日志
cat /var/log/nginx/erorr_log
重啟nginx服務(wù)
systemctl reload nginx
修改本機hosts文件
C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 www.fangwen.com
案例
指定用戶訪問 (指定用戶zmj 密碼1)
創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/zmj.conf
? server{ ? ? ? listen 80; ? ? ? server_name www.123.com; ? ? ? access_log logs/ccc_access.log; ? ? ? error_log logs/ccc_error.log; ? ? ? index index.html; ? ? ? location / { ? ? ? ? ? ? ? root /web/aaa; ? ? ? } ? ? ? location /status{ ? ? ? ? ? ? ? stub_status; ? ? ? ? ? ? ? access_log off; ? ? ? ? ? ? ? ?#auth_basic_user_file auth_basic; ? 保存密碼與驗證密碼 ? ? ? ? ? ? ? auth_basic "access auth,input your password!"; ? ? ? ? ? ? ? auth_basic_user_file /etc/nginx/auth_conf; ? ? ? } }
添加用戶
htpasswd -b -c /etc/nginx/auth_conf zmj 1
重啟nginx服務(wù)
systemctl reload nginx
修改本機hosts文件
C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 www.zmj.com
案例
指定只允許10.0.0.1訪問 拒絕其他所有訪問
創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/zmj.conf
server{ listen 80; server_name www.123.com; access_log logs/ccc_access.log; error_log logs/ccc_error.log; index index.html; location / { root /web/aaa; } location /status{ stub_status; access_log off; allow 10.0.0.1; deny all; } }
重啟nginx服務(wù)
systemctl reload nginx
修改本機hosts文件
C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 www.123.com
Nginx 訪問限制
經(jīng)常會遇到這種情況,服務(wù)器流量異常,負載過大等等。對于大流量惡意的攻擊訪問, 會帶來帶寬的浪費,服務(wù)器壓力,影響業(yè)務(wù),往往考慮對同一個 IP 的連接數(shù),并發(fā)數(shù)進行限制。
ngx_http_limit_conn_module 模塊可以根據(jù)定義的 key 來限制每個鍵值的連接數(shù),如同一個 IP 來源的連接數(shù)
limit_conn_module 連接頻率限制
limit_req_module 請求頻率限制
Client ? ? ? ? ? Server ? SYN ? ? ?------> ? ? ? ? ? <------- SYN,ACK ? ACK ? ? ? —------> ? HTTP(Req) -------> ? ? ? ? ? <-------- HTTP(Resp) ? FIN ? ? --------> ? ? ? ? ? <------- ACK
Http請求建立子啊一臺TCP連接基礎(chǔ)上,一次TCP連接至少產(chǎn)生一次HTTP請求
注:客戶端的IP地址作為鍵。
remote_addr 變量的長度為7字節(jié)到15字節(jié)
binary_remote_addr 變量的長度是固定的4字節(jié),IPv6時占用16B
如果共享內(nèi)存空間被耗盡,服務(wù)器將會對后續(xù)所有的請求返回503錯誤(Service Temporarily Unavailable)
1.Nginx 連接顯示配置實戰(zhàn)
1) Nginx 連接限制語法
Syntax: limit_conn_zone key zone=name:size; Default: — Context: http Syntax: limit_conn zone number; Default: — Context: http, server, location
2) Nginx 連接限制實戰(zhàn)
http { //http段配置連接限制, 同一時刻只允許一個客戶端IP連接 limit_conn_zone $binary_remote_addr zone=conn_zone:10m; ... server { ... location / { //同一時刻只允許一個客戶端IP連接 limit_conn conn_zone 1; }
3).使用ab工具進行壓力測試
1.吞吐率(Requests per second) 概念:服務(wù)器并發(fā)處理能力的量化描述,單位是reqs / s,指的是某個并發(fā)用戶數(shù)下單位時間內(nèi)處理的請求 數(shù)。某個并發(fā)用戶數(shù)下單位時間內(nèi)能處理的最大請求數(shù),稱之為最大吞吐率。 計算公式:總請求數(shù)/處理完成這些請求數(shù)所花費的時間,即 每秒請求數(shù)=完成請求數(shù)/測試時間 2.并發(fā)連接數(shù)(并發(fā)連接數(shù)) 概念:某個時刻服務(wù)器所接受的請求數(shù)目,簡單的講,就是一個會話。 3.并發(fā)用戶數(shù)(并發(fā)用戶數(shù),并發(fā)級別) 概念:要注意區(qū)分這個概念和并發(fā)連接數(shù)之間的區(qū)別,一個用戶可能同時會產(chǎn)生多個會話,也即連接數(shù)。 4.用戶平均請求等待時間(每個請求的時間) 計算公式:處理完成所有請求數(shù)所花費的時間/(總請求數(shù)/并發(fā)用戶數(shù)),即 每個請求的時間=測試所花費的時間/(完整請求/并發(fā)級別) 5.服務(wù)器平均請求等待時間(每個請求的時間:跨所有并發(fā)請求) 計算公式:處理完成所有請求數(shù)所花費的時間/總請求數(shù),即所花費的時間 / testsComplete請求 可以看到,它是吞吐率的倒數(shù)。 同時,它也=用戶平均請求等待時間/并發(fā)用戶數(shù),即 每個請求的時間/并發(fā)級別 ? //壓力測試 [root@web01 ~]# yum install -y httpd-tools [root@web01 ~]# ab -n 20 -c 2 //其中-n表示請求總數(shù),-c表示并發(fā)用戶數(shù),上述指令表示同時處理20個請求,并運行2次 http://127.0.0.1/index.html
2.Nginx請求限制配置實戰(zhàn)
1)Nginx請求限制語法
Syntax: limit_req_zone key zone=name:size rate=rate; Default: — Context: http Syntax: limit_conn zone number [burst=number] [nodelay]; Default: — Context: http, server, location
2)Nginx請求限制實戰(zhàn)
http { //http段配置請求限制, rate限制速率,限制一秒鐘最多一個IP請求 limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s; ... server { ... location / { //1r/s只接收一個請求,其余請求拒絕處理并返回錯誤碼給客戶端 limit_req zone=req_zone; //請求超過1r/s,剩下的將被延遲處理,請求數(shù)超過burst定義的數(shù)量, 多余的請求返回503 #limit_req zone=req_zone burst=3 nodelay;
3).使用ab工具進行壓力測試
//壓力測試 [root@web01 ~]# yum install -y httpd-tools [root@web01 ~]# ab -n 10 -c 2 http://127.0.0.1/index.html
3.nginx請求限制重定向(擴展)
在nginx請求限制的過程中,我們可以自定義一個返回值,也就是錯誤頁面的狀態(tài)碼。默認情況下是503
1)修改默認返回狀態(tài)碼
server { listen 80; server_name game.tf.com; charset utf-8,gbk; location / { root /code; index index.html index.htm; limit_req zone=req_zone burst=3 nodelay; #修改返回狀態(tài)碼為:478 limit_req_status 478 } }
2)頁面不好看,重定向頁面
server { listen 80; server_name game.tf.com; charset utf-8,gbk; location / { root /code; index index.html index.htm; limit_req zone=req_zone burst=3 nodelay; limit_req_status 478 #重定錯誤頁面 error_page 478 /err.html; } }
4.Nginx 連接限制沒有請求限制有效?
我們前面說過, 多個請求可以建立在一次的TCP連接之上, 那么我們對請求的精度限制,當然比對一個連接的限制會更加的有效
因為同一時刻只允許一個TCP連接請求進入。
但是同一時刻多個http請求可以通過一個TCP連接進入。
所以請求限制才是比較優(yōu)的解決方案。
Nginx日志配置
在學習日志之前, 我們需要先回顧下 HTTP 請求和返回
curl -v http://www.baidu.com
client ? ------(request) -----> ? ? server ? ? ? ? ? ? <-----(response) ---- ? ? ? ? request ? 包括請求行、請求頭部、請求數(shù)據(jù) ? response 包括狀態(tài)行、消息報頭、響應(yīng)正文
Nginx 有非常靈活的日志記錄模式。每個級別的配置可以有各自獨立的訪問日志。日志格式 通過log_format 命令定義格式。
1. log_format 指令
# 配置語法: 包括: error.log access.log Syntax: log_format name [escape=default|json] string ...; Default: log_format combined "..."; Context: http #默認Nginx定義日志語法 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; ? # Nginx日志格式允許包含的變量: $remote_addr # 記錄客戶端IP地址 $remote_user # 記錄客戶端用戶名 $time_local # 記錄通用的本地時間 $time_iso8601 # 記錄ISO8601標準格式下的本地時間 $request # 記錄請求的方法以及請求的http協(xié)議 $status # 記錄請求狀態(tài)碼(用于定位錯誤信息) $body_bytes_sent # 發(fā)送給客戶端的資源字節(jié)數(shù),不包括響應(yīng)頭的大小 $bytes_sent # 發(fā)送給客戶端的總字節(jié)數(shù) $msec # 日志寫入時間。單位為秒,精度是毫秒。 $http_referer # 記錄從哪個頁面鏈接訪問過來的 $http_user_agent # 記錄客戶端瀏覽器相關(guān)信息 $http_x_forwarded_for #記錄客戶端IP地址 $request_length # 請求的長度(包括請求行, 請求頭和請求正文)。 $request_time # 請求花費的時間,單位為秒,精度毫秒 # 注:如果Nginx位于負載均衡器,nginx反向代理之后, web服務(wù)器無法直接獲取到客 戶端真實的IP地 址。 # $remote_addr獲取的是反向代理的IP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中,增加x?forwarded-for信息,用來記錄客戶端IP地址和客戶端請求的服務(wù)器地址
2.access_log指令
Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; Default: access_log logs/access.log combined; Context: http, server, location, if in location, limit_except Example: server { ... access_log /var/log/nginx/www.server.com.log; ...
案例
自定義一個站點,要求如下: 1)通過域名www.nrj.com來訪問 server_name www.zmj.com; 2)文檔根目錄在/web/www目錄 root /web/www; 3)默認索引頁index.html index index.html; 4)有獨立的日志文件 access_log logs/nrj_access.log; error_log logs/nrj_error.log;
根據(jù)需求創(chuàng)建 1.創(chuàng)建目錄
mkdir -p /web/www echo 123 > /web/www/index.html ? ? #重定向 ? 把123輸入到/web/www/index.html
2.創(chuàng)建虛擬站點配置文件
vim /etc/nginx/conf.d/111.conf ? server{ ? ? ? listen 80; ? ? ? server_name www.zmj.com; ? ? ? access_log logs/zmj_access.log; ? ? ? error_log logs/zmj_error.log; ? ? ? location /{ ? ? ? ? ? ? ? root /web/www; ? ? ? ? ? ? ? index index.html; ? ? ? } }
3.檢查語法是否有錯
nginx -t ? #出現(xiàn)報錯即使修改
4.啟動服務(wù)或者重啟服務(wù)
nginx -s reload ? ? ? ? ? ?#重啟nginx服務(wù) ? systemctl reload nginx ? ? #重啟nginx服務(wù) ? 重載 systemctl restart nginx ? ?#重啟nginx服務(wù) ? ? systemctl start nginx ? ? ?#重啟nginx服務(wù) reload 和restart的區(qū)別 ? reload重載 重新加載 restart 重啟 關(guān)機后再開機 ? 可以顯示報錯信息,reload不顯示報錯信息
5.修改本機hosts文件
vim /etc/hosts #在10.0.0.7后添加 域名
10.0.0.7 www.zmj.com
6.C:\WINDOWS\system32\drivers\etc 在電腦上打開路徑 在打開的文件里面選擇hosts文件 在文件最后一行添加10.0.0.7 www.zmj.com
案例2
新建一個站點,上傳微信小游戲的程序文檔。
創(chuàng)建目錄
mkdir -p /web
把微信小游戲解壓至/web目錄下
unzip h5game.zip -d /web
創(chuàng)建虛擬站點配置文件
vim game.conf server{ listen 80; server_name game.zmj.com; access_log logs/game_access.log; error_log logs/game_error.log; location / { root /web/h5game; index index.html; } }
Nginx虛擬站點
所謂虛擬主機,及在一臺服務(wù)器上配置多個網(wǎng)站
如: 公司主頁、博客、論壇看似三個網(wǎng)站, 實則可以運行在一臺服務(wù)器上。
1.基于域名虛擬主機配置實戰(zhàn)
1)創(chuàng)建web站點目錄
[root@web01 ~]# mkdir /soft/code/{www,bbs} [root@web01 ~]# echo "www" > /soft/code/www/index.html [root@web01 ~]# echo "bbs" > /soft/code/bbs/index.html
2)配置不同域名的虛擬主機文章來源:http://www.zghlxwxcb.cn/news/detail-530125.html
[root@bgx ~]# cat /etc/nginx/conf.d/www.conf server { listen 80; server_name www.server.com; root /soft/code/www; index index.html; ... } [root@web01 ~]# cat /etc/nginx/conf.d/bbs.conf server { ... listen 80; server_name bbs.server.com; root /soft/code/bbs; index index.html; }
2.基于端口虛擬主機配置實戰(zhàn)
//僅修改listen監(jiān)聽端口即可, 但不能和系統(tǒng)端口出現(xiàn)沖突 server { ... listen 8001; ... } server { ... listen 8002; ... }
3.基于虛擬主機別名配置實戰(zhàn)
# 1.默認配置方式 [root@web01 ~]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.server.com; } server { listen 80; server_name server.com; } # 2.使用別名配置方式 [root@web01 ~]# vim /etc/nginx/conf.d/www.conf server { listen 80; server_name www.server.com server.com; ... } # 3.測試訪問, 帶www和不帶www是一樣的 [root@web01 ~]# curl server.com Go [root@web01 ~]# curl www.server.com Go
Nginx Location
使用 Nginx Location 可以控制訪問網(wǎng)站的路徑, 但一個 server 可以有多個 location 配置, 多個location 的優(yōu)先級該如何區(qū)分文章來源地址http://www.zghlxwxcb.cn/news/detail-530125.html
1.Location 語法示例
location [=|^~|~|~*|!~|!~*|/] /uri/ { ...
2.Location 語法優(yōu)先級排列
匹配符 匹配規(guī)則 優(yōu)先級 = 精確匹配 1 ^~ 以某個字符串開頭 2 ~ 區(qū)分大小寫的正則匹配 3 ~* 不區(qū)分大小寫的正則匹配 4 !~ 區(qū)分大小寫不匹配的正則 5 !~* 不區(qū)分大小寫不匹配的正則 6 通用匹配,任何請求都會匹配到 7
3.配置網(wǎng)站驗證 location 優(yōu)先級
創(chuàng)建虛擬站點配置文件 vim /etc/nginx/conf.d/111.conf server { listen 80; server_name zmj.com; location / { default_type text/html; return 200 "location /"; } location =/ { default_type text/html; return 200 "location =/"; } location ~ / { default_type text/html; return 200 "location ~/"; } }
4.測試 Location 效果
# 優(yōu)先級最高符號= [root@web01 ]# curl zmj.com location =/ # 注釋掉精確匹配=, 重啟Nginx [root@web01 ~]# curl zmj.com location ~/ # 注釋掉~, 重啟Nginx [root@web01 ~]# curl zmj.com location /
5.Location應(yīng)用場景
# 通用匹配,任何請求都會匹配到 location / { } # 嚴格區(qū)分大小寫,匹配以.php結(jié)尾的都走這個location location ~ \.php$ { fastcgi_pass http://127.0.0.1:9000; } # 嚴格區(qū)分大小寫,匹配以.jsp結(jié)尾的都走這個location location ~ \.jsp$ { proxy_pass http://127.0.0.1:8080; } # 不區(qū)分大小寫匹配,只要用戶訪問.jpg,gif,png,js,css 都走這條location location ~* .*\.(jpg|gif|png|js|css)$ { rewrite (.*) http://cdn.tftanstic.com$request_uri; } # 不區(qū)分大小寫匹配 location ~* "\.(sql|bak|tgz|tar.gz|git)$" { default_type text/html; return 403 "啟用訪問控制成功"; }
到了這里,關(guān)于Nginx日志管理、Nginx目錄索引、Nginx狀態(tài)監(jiān)控、Nginx訪問控制、訪問限制的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!