nginx變量的使用詳解
nginx 變量
Nginx 同 Apache 等其他 Web 服務器的配置記法不太相同,Nginx的配置文件使用語法的就是一門微型的編程語言。可以類似寫程序一般編寫配置文件,可操作性很大。既然是編程語言,一般也就少不了“變量”這種概念。
1、nginx變量簡介
- 所有的 Nginx變量在 Nginx 配置文件中引用時都須帶上 $ 前綴
- 在 Nginx 配置中,變量只能存放一種類型的值,有且也只存在一種類型,那就是字符串類型
nginx可以使用變量簡化配置與提高配置的靈活性,所有的變量值都可以通過這種方式引用:
$變量名
2、nginx 變量的定義和使用
nginx中的變量分為兩種,自定義變量與內(nèi)置預定義變量
1、自定義變量
1、聲明變量
可以在sever,http,location等標簽中使用set命令(非唯一)聲明變量,語法如下
set $變量名 變量值
注意:
2、變量的可見性
在不同層級的標簽中聲明的變量性的可見性規(guī)則如下:
nginx安裝echo模塊
查看已經(jīng)安裝的nginx的版本 [root@192 ~]# nginx -V 下載echo模塊的安裝包 [root@192 ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz [root@192 ~]# ls anaconda-ks.cfg nginx-1.16.0.tar.gz v0.61.tar.gz 解壓到相同路徑下: [root@192 ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/ [root@192 ~]# tar xzf v0.61.tar.gz -C /usr/local/ 安裝編譯工具 [root@192 ~]# cd /usr/local/ [root@192 local]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel 添加模塊: [root@192 local]# cd nginx-1.16.0/ 添加上原來已經(jīng)有的參數(shù)和新添加的模塊: [root@192 nginx-1.16.0]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.61 [root@192 nginx-1.16.0]# make #編譯,不要make install 否則會覆蓋原來的文件 [root@192 nginx-1.16.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_bak2 #將原來的nignx備份 [root@192 nginx-1.16.0]# cp objs/nginx /usr/local/nginx/sbin/ 拷貝nignx [root@192 nginx-1.16.0]# /usr/local/nginx/sbin/nginx #啟動 [root@192 nginx-1.16.0]# nginx -V 查看模塊是否添加成功 nginx version: nginx/1.16.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/usr/local/echo-nginx-module-0.61
3、配置 $foo=hello
- nginx 中的變量必須都以$開頭
- nginx 的配置文件中所有使用的變量都必須是聲明過的,否則 nginx 會無法啟動并打印相關異常日志
- location標簽中聲明的變量中對這個location塊可見
- server標簽中聲明的變量對server塊以及server塊中的所有子塊可見
- http標簽中聲明的變量對http塊以及http塊中的所有子塊可見
[root@192 ~]# cd /etc/nginx/conf.d/
[root@192 conf.d]# vim echo.conf
server {
listen 80;
server_name localhost;
location /test {
set $foo hello;
echo "foo: $foo";
}
}
輸出
[root@192 conf.d]# nginx -s reload [root@192 conf.d]# curl localhost/test foo: hello
5、 使用大括號插值
在“變量插值”的上下文中,還有一種特殊情況,即當引用的變量名之后緊跟著變量名的構成字符時(比如后跟字母、數(shù)字以及下劃線),我們就需要使用特別的記法來消除歧義,例如:
server {
listen 80;
server_name localhost;
location /test-brace {
set $first "hello ";
echo "${first}world";
}
}
輸出
[root@192 conf.d]# nginx -s reload [root@192 conf.d]# curl localhost/test-brace hello world
這里,我們在 echo 配置指令的參數(shù)值中引用變量 first 的時候,后面緊跟著 world 這個單詞,所以如果直接寫作 “firstworld” 則 Nginx “變量插值”計算引擎會將之識別為引用了變量 firstworld. 為了解決這個難題,Nginx 的字符串記法支持使用花括號在 之后把變量名圍起來,比如這里的 ${first}。
6、變量作用域
set 指令不僅有賦值的功能,它還有創(chuàng)建 Nginx 變量的副作用,即當作為賦值對象的變量尚不存在時,它會自動創(chuàng)建該變量。比如在上面這個例子中,如果 $a 這個變量尚未創(chuàng)建,則 set 指令會自動創(chuàng)建 $a 這個用戶變量。如果我們不創(chuàng)建就直接使用它的值,則會報錯。
例如
server {
...
location /bad {
echo $foo;
}
}
此時 Nginx 服務器會拒絕加載配置:
[root@192 conf.d]# nginx -s reload nginx: [emerg] unknown "foo" variable nginx: configuration file /etc/nginx/nginx.conf test failed
Nginx 變量的創(chuàng)建和賦值操作發(fā)生在全然不同的時間階段,Nginx 變量的創(chuàng)建只能發(fā)生在 Nginx 配置加載的時候,或者說 Nginx 啟動的時候,而賦值操作則只會發(fā)生在請求實際處理的時候。
這意味著不創(chuàng)建而直接使用變量會導致啟動失敗,同時也意味著我們無法在請求處理時動態(tài)地創(chuàng)建新的 Nginx 變量。
Nginx 變量一旦創(chuàng)建,其變量名的可見范圍就是整個 Nginx 配置,甚至可以跨越不同虛擬主機的 server 配置塊。我們來看一個例子:
server {
listen 80;
server_name localhost;
location /foo {
echo "foo = [$foo]";
}
location /bar {
set $foo 32;
echo "foo = [$foo]";
}
}
輸出
[root@192 conf.d]# curl 'http://localhost/foo' foo = [] [root@192 conf.d]# curl 'http://localhost/bar' foo = [32]
這里我們在 location /bar 中用 set 指令創(chuàng)建了變量 foo,于是在整個配置文件中這個變量都是可見的,因此我們可以在 location /foo 中直接引用這個變量而不用擔心 Nginx 會報錯。
從這個例子我們可以看到,set 指令因為是在 location /bar 中使用的,所以賦值操作只會在訪問 /bar 的請求中執(zhí)行。而請求 /foo 接口時,我們總是得到空的 foo值,因為用戶變量未賦值就輸出的話,得到的便是空字符串。
從這個例子我們可以窺見的另一個重要特性是,Nginx 變量名的可見范圍雖然是整個配置,但每個請求都有所有變量的獨立副本,或者說都有各變量用來存放值的容器的獨立副本,彼此互不干擾。比如前面我們請求了 /bar 接口后,foo 變量被賦予了值 32,但它絲毫不會影響后續(xù)對 /foo 接口的請求所對應的 foo 值(它仍然是空的?。驗楦鱾€請求都有自己獨立的 $foo 變量的副本。
2、內(nèi)置預定義變量
內(nèi)置預定義變量即無需聲明就可以使用的變量,通常包括一個http請求或響應中一部分內(nèi)容的值,以下為一些常用的內(nèi)置預定義變量
| 變量名 | 定義 |
|---|---|
| $arg_PARAMETER | GET請求中變量名PARAMETER參數(shù)的值。 |
| $args | 這個變量等于GET請求中的參數(shù)。例如,foo=123&bar=blahblah;這個變量只可以被修改 |
| $binary_remote_addr | 二進制碼形式的客戶端地址。 |
| $body_bytes_sent | 傳送頁面的字節(jié)數(shù) |
| $content_length | 請求頭中的Content-length字段。 |
| $content_type | 請求頭中的Content-Type字段。 |
| $cookie_COOKIE | cookie COOKIE的值。 |
| $document_root | 當前請求在root指令中指定的值。 |
| $document_uri | 與$uri相同。 |
| $host | 請求中的主機頭(Host)字段,如果請求中的主機頭不可用或者空,則為處理請求的server名稱(處理請求的server的server_name指令的值)。值為小寫,不包含端口。 |
| $hostname | 機器名使用 gethostname系統(tǒng)調(diào)用的值 |
| $http_HEADER | HTTP請求頭中的內(nèi)容,HEADER為HTTP請求中的內(nèi)容轉為小寫,-變?yōu)開(破折號變?yōu)橄聞澗€),例如:$http_user_agent(Uaer-Agent的值); |
| $sent_http_HEADER | HTTP響應頭中的內(nèi)容,HEADER為HTTP響應中的內(nèi)容轉為小寫,-變?yōu)開(破折號變?yōu)橄聞澗€),例如: $sent_http_cache_control, $sent_http_content_type…; |
| $is_args | 如果$args設置,值為"?",否則為""。 |
| $limit_rate | 這個變量可以限制連接速率。 |
| $nginx_version | 當前運行的nginx版本號。 |
| $query_string | 與$args相同。 |
| $remote_addr | 客戶端的IP地址。 |
| $remote_port | 客戶端的端口。 |
| $remote_user | 已經(jīng)經(jīng)過Auth Basic Module驗證的用戶名。 |
| $request_filename | 當前連接請求的文件路徑,由root或alias指令與URI請求生成。 |
| $request_body | 這個變量(0.7.58+)包含請求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比較有意義。 |
| $request_body_file | 客戶端請求主體信息的臨時文件名。 |
| $request_completion | 如果請求成功,設為"OK";如果請求未完成或者不是一系列請求中最后一部分則設為空。 |
| $request_method | 這個變量是客戶端請求的動作,通常為GET或POST。包括0.8.20及之前的版本中,這個變量總為main request中的動作,如果當前請求是一個子請求,并不使用這個當前請求的動作。 |
| $request_uri | 這個變量等于包含一些客戶端請求參數(shù)的原始URI,它無法修改,請查看$uri更改或重寫URI。 |
| $scheme | 所用的協(xié)議,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect; |
| $server_addr | 服務器地址,在完成一次系統(tǒng)調(diào)用后可以確定這個值,如果要繞開系統(tǒng)調(diào)用,則必須在listen中指定地址并且使用bind參數(shù)。 |
| $server_name | 服務器名稱。 |
| $server_port | 請求到達服務器的端口號。 |
| $server_protocol | 請求使用的協(xié)議,通常是HTTP/1.0或HTTP/1.1。 |
| $uri | 請求中的當前URI(不帶請求參數(shù),參數(shù)位于args,不同于瀏覽器傳遞的args),不同于瀏覽器傳遞的request_uri的值,它可以通過內(nèi)部重定向,或者使用index指令進行修改。不包括協(xié)議和主機名,例如/foo/bar.html |
Nginx 內(nèi)置變量最常見的用途就是獲取關于請求或響應的各種信息。
1、uri vs request_uri
由 ngx_http_core 模塊提供的內(nèi)建變量 uri,可以用來獲取當前請求的 URI(不含請求參數(shù)),而 request_uri 則用來獲取請求最原始的 URI(包含請求參數(shù))。
server {
listen 80;
server_name localhost;
location /test-uri {
echo "uri = $uri";
echo "request_uri = $request_uri";
}
}
輸出
[root@localhost html]# nginx -s reload [root@192 conf.d]# nginx -s reload [root@192 conf.d]# curl localhost/test-uri uri = /test-uri request_uri = /test-uri [root@192 conf.d]# curl "localhost/test-uri?a=3&b=4" uri = /test-uri request_uri = /test-uri?a=3&b=4 [root@192 conf.d]# curl "localhost/test-uri/hello%20world?a=3&b=4" uri = /test-uri/hello world request_uri = /test-uri/hello%20world?a=3&b=4
2、$arg_XXX
另一個特別常用的內(nèi)建變量其實并不是單獨一個變量,而是有無限多變種的一群變量,即名字以 arg_ 開頭的所有變量,我們估且稱之為 arg_XXX 變量群。
一個例子是 arg_name,這個變量的值是當前請求中名為 name 的參數(shù)的值,而且還是未解碼的原始形式的值。
server {
listen 80;
server_name localhost;
location /test-arg {
echo "name: $arg_name";
echo "class: $arg_class";
}
}
輸出
[root@192 conf.d]# nginx -s reload [root@192 conf.d]# curl localhost/test-arg name: class: [root@192 conf.d]# curl "localhost/test-arg?name=Tom&class=3" name: Tom class: 3 [root@192 conf.d]# curl "localhost/test-arg?name=hello%20world&class=9" name: hello%20world class: 9
3、$arg_XXX 不區(qū)分大小寫
其實 $arg_name 不僅可以匹配 name 參數(shù),也可以匹配 NAME 參數(shù),抑或是 Name,Nginx 會在匹配參數(shù)名之前,自動把原始請求中的參數(shù)名調(diào)整為全部小寫的形式。
[root@192 conf.d]# curl "localhost/test-arg?NAME=Marry" name: Marry class: [root@192 conf.d]# curl "localhost/test-arg?Name=Jimmy&class=DSfef" name: Jimmy class: DSfef
nginx 監(jiān)控
1、nginx的基礎監(jiān)控
- 進程監(jiān)控
- 端口監(jiān)控
注意: 這兩個是必須要加在zabbix監(jiān)控,加觸發(fā)器有問題及時告警。
web 服務器 nginx 以其高性能與抗并發(fā)能力越來越多的被用戶使用
nginx 提供了 ngx_http_stub_status_module,ngx_http_reqstat_module模塊,這個模塊提供了基本的監(jiān)控功能
2、監(jiān)控的主要指標
我們需要對以下主要的指標進行監(jiān)控:
1、基本活躍指標
Accepts(接受)、Handled(已處理)、Requests(請求數(shù))是一直在增加的計數(shù)器。Active(活躍)
| 名稱 | 描述 | 指標類型 |
|---|---|---|
| Accepts(接受) | NGINX 所接受的客戶端連接數(shù) | 資源: 功能 |
| Handled(已處理) | 成功的客戶端連接數(shù) | 資源: 功能 |
| Dropped(已丟棄,計算得出) | 丟棄的連接數(shù)(接受 - 已處理) | 工作:錯誤* |
| Requests(請求數(shù)) | 客戶端請求數(shù) | 工作:吞吐量 |
2、每秒請求數(shù) – QPS
通過持續(xù)的 QPS 監(jiān)控,可以立刻發(fā)現(xiàn)是否被惡意攻擊或?qū)Ψ盏目捎眯赃M行評估。雖然當問題發(fā)生時,通過 QPS 不能定位到確切問題的位置,但是他卻可以在第一時間提醒你環(huán)境可能出問題了
3、服務器錯誤率
通過監(jiān)控固定時間間隔內(nèi)的錯誤代碼(4XX代碼表示客戶端錯誤,5XX代碼表示服務器端錯誤),可以了解到客戶端收到的結果是否是正確的錯誤率突然的飆升很可能是你的網(wǎng)站漏洞發(fā)出的信號
如果你希望通過 access log 分析錯誤率,那么你需要配置 nginx 的日志模塊,讓 nginx 將響應碼寫入訪問日志
3、指標的收集
通過在編譯時加入 nginx 的 ngx_http_stub_status_module 模塊我們可以實時監(jiān)控以下基本的指標:
1、nginx Stub Status 監(jiān)控模塊安裝
先使用命令查看是否已經(jīng)安裝這個模塊:
# -V大寫會顯示版本號和模塊等信息、v小寫僅顯示版本信息 [root@localhost ~]# nginx -V
如果沒有此模塊,需要重新安裝,編譯命令如下:
./configure –with-http_stub_status_module
具體的使用方法是在執(zhí)行 ./configure 時,指定 --with-http_stub_status_module,然后通過配置:
server {
listen 80;
server_name localhost;
location /nginx-status {
stub_status on;
access_log on;
}
}
2、nginx 狀態(tài)查看
配置完成后在瀏覽器中輸入http://10.0.105.207/nginx-status 查看
(或者用 curl localhost/nginx-status),顯示信息如下:
Active connections: 2 server accepts handled requests 26 26 48 Reading: 0 Writing: 1 Waiting: 1
3、Stub Status 參數(shù)說明
正常情況下waiting數(shù)量是比較多的,并不能說明性能差。如果reading+writing數(shù)量比較多說明服務并發(fā)有問題。
Active connections:2 #當前nginx處理請求的數(shù)目(活躍的連接數(shù)) server accepts handled requests 26 26 48
nginx總共處理了26個連接,成功創(chuàng)建26次握手,也就是成功的連接數(shù)connection. 總共處理了48個請求
失敗連接=(總連接數(shù)-成功連接數(shù))(相等表示中間沒有失敗的),
5、Reqstat 模塊監(jiān)控 ----已經(jīng)不支持了(需要導入)
描述
- ngx_http_reqstat_module 模塊
- 這個模塊計算定義的變量,根據(jù)變量值分別統(tǒng)計 nginx 的運行狀況。
- 可以監(jiān)視的運行狀況有:連接數(shù)、請求數(shù)、各種響應碼范圍的請求數(shù)、輸入輸出流量、rt、upstream訪問等。
- 可以指定獲取所有監(jiān)控結果或者一部分監(jiān)控結果。
- 利用變量添加自定義監(jiān)控狀態(tài)??偟谋O(jiān)控狀態(tài)最大個數(shù)為50個。
- 回收過期的監(jiān)控數(shù)據(jù)。
- 設置輸出格式
- 跟蹤請求,不受內(nèi)部跳轉的影響
- 不要使用與響應相關的變量作為條件,比如"$status"
現(xiàn)在通過ngx_req_status_module能夠統(tǒng)計Nginx中請求的狀態(tài)信息。需要安裝第三方模塊
安裝模塊:
tengine官方說req-status模塊默認安裝。但是并沒有。從github引入第三方模塊解決該問題
yum與編譯安裝的nginx擴展模塊安裝:
[root@nginx-server ~]# yum install -y unzip
1. 安裝,先查看一下當前編譯安裝nginx的版本
[root@localhost nginx-1.16.0]# nginx -V
下載或者上傳一個和當前的nginx版本一樣的nginx的tar包。
[root@nginx-server ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/
2.下載ngx_req_status_module 模塊, 這是第三方模塊需要添加
[root@nginx-server ~]# wget https://github.com/zls0424/ngx_req_status/archive/master.zip -O ngx_req_status.zip
[root@nginx-server ~]# unzip ngx_req_status.zip
[root@nginx-server ~]# cp -r ngx_req_status-master/ /usr/local/ #與解壓的nginx在同一級目錄下
[root@nginx-server ~]# cd /usr/local/nginx-1.16.0/
[root@nginx-server nginx-1.16.0]# yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ zlib zlib-devel
[root@nginx-server nginx-1.16.0]# yum -y install patch.x86_64
[root@nginx-server nginx-1.16.0]# patch -p1 < ../ngx_req_status-master/write_filter-1.7.11.patch
[root@localhost nginx-1.16.0]# ./configure 添加上原來的參數(shù) --add-module=/usr/local/ngx_req_status-master
[root@localhost nginx-1.16.0]# make
由于原先已有nginx,所以不能執(zhí)行make install,否則會覆蓋掉以前的配置文件及內(nèi)容
[root@localhost nginx-1.16.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak
[root@localhost nginx-1.16.0]# cp objs/nginx /usr/sbin/
[root@localhost nginx-1.16.0]# systemctl restart nginx
[root@localhost nginx-1.16.0]# nginx -V
如果發(fā)現(xiàn)編譯的配置文件有變化就成功了!
配置如下: 需要在http里面配置。
[root@localhost ~]# vim /etc/nginx/nginx.conf
req_status_zone server_name $server_name 256k;
req_status_zone server_addr $server_addr 256k;
req_status_zone server_url $server_name$uri 256k;
req_status server_name server_addr server_url;
server {
server_name localhost;
location /req-status {
req_status_show on;
}
}
指令介紹
req_status_zone
語法: req_status_zone name string size
默認值: None
配置塊: http
定義請求狀態(tài)ZONE,請求按照string分組來排列,例如:
req_status_zone server_url $server_name$uri 256k;
域名+uri將會形成一條數(shù)據(jù),可以看到所有url的帶寬,流量,訪問數(shù)
req_status
語法: req_status zone1[ zone2]
默認值: None
配置塊: http, server, location
在location中啟用請求狀態(tài),你可以指定更多zones。
req_status_show
語法: req_status_show on
默認值: None
配置塊: location
在當前位置啟用請求狀態(tài)處理程序
請求狀態(tài)信息包括以下字段:
- zone_name - 利用req_status_zone定義的分組標準。例如,按照服務器名稱對請求進行分組后;
- key - 請求按分組標準分組后的分組標識(即組名)。例如按服務器名稱分組時,組名可能是localhost;
- max_active - 該組的最大并發(fā)連接數(shù);
- max_bw - 該組的最大帶寬;
- traffic - 該組的總流量;
- requests - 該組的總請求數(shù);
- active - 該組當前的并發(fā)連接數(shù);
- bandwidth - 該組當前帶寬。
6、補充(擴展):
nginx access log 分析
nginx 的 access log 中可以記錄很多有價值的信息,通過分析 access log,可以收集到很多指標
1.制作nginx的日志切割,每天凌晨切割并壓縮。
- PV:PV(訪問量): 即Page View, 即頁面瀏覽量或點擊量,用戶每次刷新即被計算一次。
- UV:UV(獨立訪客):即Unique Visitor,訪問您網(wǎng)站的一臺電腦客戶端為一個訪客。00:00-24:00內(nèi)相同的客戶端只被計算一次。
工作常用(面試題筆試):
1.根據(jù)訪問IP統(tǒng)計UV
awk '{print $1}' access.log|sort | uniq -c |wc -l
2.統(tǒng)計訪問URL統(tǒng)計PV
awk '{print $7}' access.log|wc -l
3.查詢訪問最頻繁的URL
awk '{print $7}' access.log|sort | uniq -c |sort -n -k 1 -r|more
4.查詢訪問最頻繁的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|more
5.查詢訪問最頻繁的前10的IP
awk '{print $1}' access.log|sort | uniq -c |sort -n -k 1 -r|head -n 10
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Nginx之Http模塊系列之a(chǎn)utoindex模塊的具體使用
這篇文章主要介紹了Nginx之Http模塊系列之a(chǎn)utoindex模塊的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
18個運維必知的Nginx代理緩存配置技巧(你都掌握了哪些呢)
這篇文章主要介紹了18個運維必知的Nginx代理緩存配置技巧(你都掌握了哪些呢),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
詳解基于centos7搭建Nginx網(wǎng)站服務器(包含虛擬web主機的配置)
這篇文章主要介紹了詳解基于centos7搭建Nginx網(wǎng)站服務器(包含虛擬web主機的配置),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-10-10
Centos7安裝、卸載nginx及配置,配置成系統(tǒng)服務方式(一步到位)
這篇文章主要介紹了Centos7安裝、卸載nginx及配置,配置成系統(tǒng)服務方式(一步到位),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

