国产欧美日韩第一页|日本一二三不卡视频|在线精品小视频,亚洲第一免费播放区,metcn人体亚洲一区,亚洲精品午夜视频

幫助中心 >  技術(shù)知識庫 >  云服務(wù)器 >  服務(wù)器教程 >  Nginx訪(fǎng)問(wèn)日志access_log配置及信息

Nginx訪(fǎng)問(wèn)日志access_log配置及信息

2024-05-11 17:36:49 482

Nginx訪(fǎng)問(wèn)日志access_log配置及信息


藍隊云小課堂:

Nginx訪(fǎng)問(wèn)日志access_log配置及信息

Nginx訪(fǎng)問(wèn)日志主要有兩個(gè)參數控制:

log_format #用來(lái)定義記錄日志的格式(可以定義多種日志格式,取不同名字即可)

access_log #用來(lái)指定日至文件的路徑及使用的何種日志格式記錄日志

#    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

#                      '$status $body_bytes_sent "$http_referer" '

#                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log的默認值:

#access_log  logs/access.log  main;

log_format log_format語(yǔ)法格式及參數語(yǔ)法說(shuō)明如下:

log_format    <NAME>    <String>;

    關(guān)鍵字     格式標簽   日志格式

    關(guān)鍵字:其中關(guān)鍵字error_log不能改變

    格式標簽:格式標簽是給一套日志格式設置一個(gè)獨特的名字

    日志格式:給日志設置格式

作用域    :    http

eg:

log_format compression '$remote_addr - $remote_user [$time_local] '

                       '"$request" $status $bytes_sent '

                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log /spool/logs/nginx-access.log compression buffer=32k;

log_format格式變量:

參數                      說(shuō)明                                         示例

$remote_addr             客戶(hù)端地址                                    211.28.65.253

$remote_user             客戶(hù)端用戶(hù)名稱(chēng)                                --

$time_local              訪(fǎng)問(wèn)時(shí)間和時(shí)區                                18/Jul/2012:17:00:01 +0800

$request                 請求的URI和HTTP協(xié)議                           "GET /article-10000.html HTTP/1.1"

$http_host               請求地址,即瀏覽器中你輸入的地址(IP或域名)     www.tjdsmy.cn 192.168.100.100

$status                  HTTP請求狀態(tài)                                  200

$upstream_status         upstream狀態(tài)                                  200

$body_bytes_sent         發(fā)送給客戶(hù)端文件內容大小                        1547

$http_referer            url跳轉來(lái)源                                   http://www.tjdsmy.cn/

$http_user_agent         用戶(hù)終端瀏覽器等信息                           "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;

$ssl_protocol            SSL協(xié)議版本                                   TLSv1

$ssl_cipher              交換數據中的算法                               RC4-SHA

$upstream_addr           后臺upstream的地址,即真正提供服務(wù)的主機地址     10.10.10.100:80

$request_time            整個(gè)請求的總時(shí)間                               0.205

$upstream_response_time  請求過(guò)程中,upstream響應時(shí)間                    0.002

access_log

語(yǔ)法格式及參數語(yǔ)法說(shuō)明如下:

    access_log    <FILE>    <NAME>;

    關(guān)鍵字         日志文件   格式標簽

    關(guān)鍵字:其中關(guān)鍵字error_log不能改變

    日志文件:可以指定任意存放日志的目錄

    格式標簽:給日志文件套用指定的日志格式

其他語(yǔ)法:

    access_log    off;  #關(guān)閉access_log,即不記錄訪(fǎng)問(wèn)日志

    access_log path [format [buffer=size [flush=time]] [if=condition]];

    access_log path format gzip[=level] [buffer=size] [flush=time] [if=condition];

    access_log syslog:server=address[,parameter=value] [format [if=condition]];

    說(shuō)明:

    buffer=size  #為存放訪(fǎng)問(wèn)日志的緩沖區大小

    flush=time  #為緩沖區的日志刷到磁盤(pán)的時(shí)間

    gzip[=level]  #表示壓縮級別

    [if = condition]  #表示其他條件

作用域(參數的標簽段位置)   : http, server, location, if in location, limit_except

eg:

access_log /spool/logs/nginx-access.log compression buffer=32k;

open_log_file_cache

使用open_log_file_cache來(lái)設置日志文件緩存(默認是off)。

max:設置緩存中的最大文件描述符數量,如果緩存被占滿(mǎn),采用LRU算法將描述符關(guān)閉。

inactive:設置存活時(shí)間,默認是10s

min_uses:設置在inactive時(shí)間段內,日志文件最少使用多少次后,該日志文件描述符記入緩存中,默認是1次

valid:設置檢查頻率,默認60s

off:禁用緩存

語(yǔ)法格式:   open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

                     open_log_file_cache off;

默認值:     open_log_file_cache off;

作用域:     http, server, location

eg:

open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

參考資料:http://www.tjdsmy.cn/en/docs/http/ngx_http_log_module.html

nginx日志調試技巧

設置 Nginx 僅記錄來(lái)自于你的 IP 的錯誤

當你設置日志級別成 debug,如果你在調試一個(gè)在線(xiàn)的高流量網(wǎng)站的話(huà),你的錯誤日志可能會(huì )記錄每個(gè)請求的很多消息,這樣會(huì )變得毫無(wú)意義。

在events{…}中配置如下內容,可以使 Nginx 記錄僅僅來(lái)自于你的 IP 的錯誤日志。

events {

        debug_connection 1.2.3.4;

}

調試 nginx rewrite 規則

調試rewrite規則時(shí),如果規則寫(xiě)錯只會(huì )看見(jiàn)一個(gè)404頁(yè)面,可以在配置文件中開(kāi)啟nginx rewrite日志,進(jìn)行調試。

server {

        error_log    /var/logs/nginx/example.com.error.log;

        rewrite_log on;

}

rewrite_log on; 開(kāi)啟后,它將發(fā)送所有的 rewrite 相關(guān)的日志信息到 error_log 文件中,使用 [notice] 級別。隨后就可以在error_log 查看rewrite信息了。

使用location記錄指定URL的日志

server {

        error_log    /var/logs/nginx/example.com.error.log;

        location /static/ { 

        error_log /var/logs/nginx/static-error.log debug; 

    }         

}

Nginx配置訪(fǎng)問(wèn)日志過(guò)程:

(1)創(chuàng )建log_format語(yǔ)句

worker_processes  1;

error_log logs/error.log error;

events {

    worker_connections  1024;

}

http {

    include status.conf;

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                               '$status $body_bytes_sent "$http_referer" '

                               '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    server {

        listen       80;

        server_name  localhost;

                rewrite ^/.* http://www.tjdsmy.cn permanent;

    }

    include vhost/*.conf;

}

(2)插入access_log語(yǔ)句

server {

        access_log /data/log/www;

        listen 80;

        server_name abc.com www.tjdsmy.cn;

        location / {

                root /data/www/www;

                index index.html index.htm;

        }

        error_log    logs/error_www.tjdsmy.cn    error;

        access_log    logs/access_www.tjdsmy.cn    main;

        #新增內容↑

}

(3)重啟服務(wù)

nginx -t

nginx -s reload

常用例子

main格式

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"'

                       '$upstream_addr $upstream_response_time $request_time ';

access_log  logs/access.log  main;

json格式

log_format logstash_json '{"@timestamp":"$time_iso8601",'

       '"host": "$server_addr",'

       '"client": "$remote_addr",'

       '"size": $body_bytes_sent,'

       '"responsetime": $request_time,'

       '"domain": "$host",'

       '"url":"$request_uri",'

       '"referer": "$http_referer",'

       '"agent": "$http_user_agent",'

       '"status":"$status",'

       '"x_forwarded_for":"$http_x_forwarded_for"}';

解釋?zhuān)?/span>

uri請求中的當前uri(不帶請求參數,參數位于uri請求中的當前URI(不帶請求參數,參數位于 uri請求中的當前URI(不帶請求參數,參數位于args),不同于瀏覽器傳遞的$request_uri的值,它可以通過(guò)內部重定向,或者使用index指令進(jìn)行修改。不包括協(xié)議和主機名,例如/foo/bar.html。

requesturi這個(gè)變量等于包含一些客戶(hù)端請求參數的原始uri,它無(wú)法修改,請查看request—_uri這個(gè)變量等于包含一些客戶(hù)端請求參數的原始URI,它無(wú)法修改,請查看 requesturi這個(gè)變量等于包含一些客戶(hù)端請求參數的原始URI,它無(wú)法修改,請查看uri更改或重寫(xiě)URI。

也就是說(shuō):requesturi是原始請求url,request_uri是原始請求URL, requesturi是原始請求URL,uri則是經(jīng)過(guò)nginx處理請求后剔除參數的URL,所以會(huì )將漢字表現為union。

坑點(diǎn):

使用uri可以在nginx對url進(jìn)行更改或重寫(xiě),但是用于日志輸出可以使用uri可以在nginx對URL進(jìn)行更改或重寫(xiě),但是用于日志輸出可以使用 uri可以在nginx對URL進(jìn)行更改或重寫(xiě),但是用于日志輸出可以使用request_uri代替,如無(wú)特殊業(yè)務(wù)需求,完全可以替換。

壓縮格式

日志中增加了壓縮的信息。

http {

    log_format compression '$remote_addr - $remote_user [$time_local] '

                           '"$request" $status $body_bytes_sent '

                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

    server {

        gzip on;

        access_log /spool/logs/nginx-access.log compression;

        ...

    }

}

upstream格式

增加upstream消耗的時(shí)間。

http {

    log_format upstream_time '$remote_addr - $remote_user [$time_local] '

                             '"$request" $status $body_bytes_sent '

                             '"$http_referer" "$http_user_agent"'

                             'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';

    server {

        access_log /spool/logs/nginx-access.log upstream_time;

        ...

    }

}

統計信息

統計status 出現的次數

awk '{print $9}' access.log | sort | uniq -c | sort -rn

36461 200 

483 500

87 404

9 400

3 302

1 499

1 403

1 301

顯示返回302狀態(tài)碼的URL。

awk '($9 ~ /302/)' access.log | awk '{print $7}' | sort | uniq -c | sort -rn

1 /wp-login.php

1 /wp-admin/plugins.php?action=activate&plugin=ewww-image-optimizer%2Fewww-image-optimizer.php&_wpnonce=cc4a379131

1 /wp-admin/

更多小知識,可聯(lián)系藍隊云一起探討。


提交成功!非常感謝您的反饋,我們會(huì )繼續努力做到更好!

這條文檔是否有幫助解決問(wèn)題?

非常抱歉未能幫助到您。為了給您提供更好的服務(wù),我們很需要您進(jìn)一步的反饋信息:

在文檔使用中是否遇到以下問(wèn)題:
-->