欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx日志輸出配置json格式

 更新時間:2024年07月15日 11:45:46   作者:富士康質(zhì)檢員張全蛋  
本文主要介紹了Nginx日志輸出配置json格式,包含log_format和access_log兩種命令,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

nginx服務(wù)器日志相關(guān)指令主要有兩條:

(1) 一條是log_format,用來設(shè)置日志格式

(2) 另外一條是access_log,用來指定日志文件的存放路徑、格式和緩存大小。

log_format指令用來設(shè)置日志的記錄格式,它的語法如下:
log_format name format {format ...} 其中name表示定義的格式名稱,format表示定義的格式樣

網(wǎng)上統(tǒng)一方法: 修改nginx.conf配置文件

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    
    # 原有日志格式,不能注釋或者去掉
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_time';
    # json日志格式
    log_format log_json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"x_forwarded": "$http_x_forwarded_for", '
                        '"up_addr": "$upstream_addr",'
                        '"up_host": "$upstream_http_host",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        ' }';
 
    access_log  logs/access.log log_json; # 引用日志格式名稱
 
    (省略內(nèi)容)
}

在 Nginx 的配置文件nginx.conf中,我們定義了兩種的日志格式:main和log_json,其中main為普通的文本格式,log_json為 json 格式。

log_json其實就是手工構(gòu)造一個 json 字符串。定義了 json 的日志格式后,便可以指定 access log 為 json 格式,修改 Nginx 的配置,重啟 Nginx ,便可以看到 json 格式的日志,重啟 Nginx。

附加:可能遇到的問題

背景:
  因為我這邊是通過nginx去代理了很多域名,所以會有很多平臺訪問的日志,通過在nginx.conf主配置文件增加  include vhosts/*.conf; 導(dǎo)入主機配置,所以會存在一個server段中只能有一個名稱的問題。 

正確操作:

修改nginx.conf配置文件

[root@elk-nginx-01 conf]# cd /data/services/nginx/conf/
[root@elk-nginx-01 conf]# pwd
/data/services/nginx/conf
[root@elk-nginx-01 conf]# ll
總用量 72
-rw-r--r-- 1 root root 1077 9月   3 09:45 fastcgi.conf
-rw-r--r-- 1 root root 1077 9月   3 09:45 fastcgi.conf.default
-rw-r--r-- 1 root root 1007 9月   3 09:45 fastcgi_params
-rw-r--r-- 1 root root 1007 9月   3 09:45 fastcgi_params.default
-rw-r--r-- 1 root root 2837 9月   3 09:45 koi-utf
-rw-r--r-- 1 root root 2223 9月   3 09:45 koi-win
-rw-r--r-- 1 root root 5231 9月   3 09:45 mime.types
-rw-r--r-- 1 root root 5231 9月   3 09:45 mime.types.default
-rw-r--r-- 1 root root 3729 9月   3 15:49 nginx.conf
-rw-r--r-- 1 root root 2656 9月   3 13:59 nginx.conf-bak0903
-rw-r--r-- 1 root root 2656 9月   3 09:45 nginx.conf.default
-rw-r--r-- 1 root root  636 9月   3 09:45 scgi_params
-rw-r--r-- 1 root root  636 9月   3 09:45 scgi_params.default
drwxr-xr-x 2 root root  134 9月   3 11:34 sslkeys
-rw-r--r-- 1 root root  664 9月   3 09:45 uwsgi_params
-rw-r--r-- 1 root root  664 9月   3 09:45 uwsgi_params.default
drwxr-xr-x 2 root root   87 9月   3 16:53 vhosts
-rw-r--r-- 1 root root 3610 9月   3 09:45 win-utf
[root@elk-nginx-01 conf]# vim nginx.conf
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    charset  utf-8;
    
    # 原有日志格式,不能注釋
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" $request_time';
    # json日志格式
    log_format json '{"@timestamp": "$time_local", '
                        '"remote_addr": "$remote_addr", '
                        '"referer": "$http_referer", '
                        '"request": "$request", '
                        '"status": $status, '
                        '"bytes": $body_bytes_sent, '
                        '"agent": "$http_user_agent", '
                        '"x_forwarded": "$http_x_forwarded_for", '
                        '"up_addr": "$upstream_addr",'
                        '"up_host": "$upstream_http_host",'
                        '"up_resp_time": "$upstream_response_time",'
                        '"request_time": "$request_time"'
                        ' }';
 
    #導(dǎo)入主機配置
    include vhosts/*.conf;
 
    (省略內(nèi)容)
}
保存退出!
 
[root@elk-nginx-01 conf]# pwd
/data/services/nginx/conf
[root@elk-nginx-01 conf]# cd vhosts/
[root@elk-nginx-01 vhosts]# ll
總用量 12
-rw-r--r-- 1 yfbkf yfbkf  921 9月   3 15:37 gonggao.conf
-rw-r--r-- 1 yfbkf yfbkf 1482 9月   3 16:50 gongdan.conf
-rw-r--r-- 1 yfbkf yfbkf 1151 9月   3 14:42 gongmo.conf

然后重啟nginx

總結(jié):
1、原有的日志格式不能注釋或者去掉,只能新增一個log_format
2、新增自定義一份日志記錄格式,需要注意,log_format指令設(shè)置的名稱在配置文件中是不能重復(fù)的(比如我json日志格式名稱 json)
3、原有的日志格式如果調(diào)整了,需要在log后加上名稱才生效

Nginx日志常用參數(shù)詳解

    log_format json '{"@timestamp":"$time_iso8601",'
                     '"scheme":"$scheme",'
                     '"http_referer":"$http_referer",'
                     '"args":"$args",'
                     '"http_user_agent":"$http_user_agent",'
                     '"remote_addr":"$remote_addr",'
                     '"hoste":"$host",'
                     '"server_name":"$server_name",'
                     '"server_protocol":"$server_protocol",'
                     '"request_method":"$request_method",'
                     '"request_uri":"$request_uri",'
                     '"uri":"$uri",'
                     '"request_length":"$request_length",'
                     '"body_byte_sent": "$body_bytes_sent",'
                     '"request_time":"$request_time",'
                     '"server_addr":"$server_addr",'
                     '"status": $status,'
                     '"bytes_sent":"$bytes_sent",'
                     '"upstream_addr":"$upstream_addr",'
                     '"upstream_status":"$upstream_status",'
                     '"upstream_connect_time":"$upstream_connect_time",'
                     '"upstream_response_time":"$upstream_response_time",'
                     '"request_id":"$request_id"'
                     '}';

可以加上這些:
$request_filename:當(dāng)前請求的文件路徑,由root或alias指令與URI請求生成。
$http_cookie:客戶端cookie信息
$http_host #請求地址,即瀏覽器中你輸入的地址(IP或域名)
$server_port:請求到達(dá)服務(wù)器的端口號。
$connection_requests 當(dāng)前通過一個連接獲得的請求數(shù)量。
  • $remote_addr:記錄訪問網(wǎng)站的客戶端地址

$remote_user:遠(yuǎn)程客戶端用戶名稱
$time_local:記錄訪問時間與時區(qū)
$request:表示request請求頭的行

  • $status:http狀態(tài)碼,記錄請求返回的狀態(tài),例如:200、404、301等
  • $body_bytes_sent:服務(wù)器發(fā)送給客戶端的響應(yīng)body字節(jié)數(shù),發(fā)送給客戶端的文件主體內(nèi)容的大小,比如899,可以將日志每條記錄中的這個值累加起來以粗略估計服務(wù)器吞吐量。

$http_referer:記錄此次請求是從哪個鏈接訪問過來的,可以根據(jù)refer進(jìn)行防盜鏈設(shè)置
$http_user_agent:記錄客戶端訪問信息,例如:瀏覽器,手機客戶端等

  • $http_x_forwarded_for:客戶端的真實ip,通常web服務(wù)器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理服務(wù)器的iP地址。反向代理服務(wù)器在轉(zhuǎn)發(fā)請求的http頭信息中,可以增加x_forwarded_for信息,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務(wù)器地址。

$http_host 請求地址,即瀏覽器中你輸入的地址(IP或域名)
$ssl_protocol:SSL協(xié)議版本
$ssl_cipher:交換數(shù)據(jù)中的算法
$upstream_status:upstream狀態(tài),比如成功是200
$upstream_addr:當(dāng)ngnix做負(fù)載均衡時,可以查看后臺提供真實服務(wù)的設(shè)備
$upstream_response_time:請求過程中,upstream響應(yīng)時間

  • $request_time:整個請求的總時間,請求處理時間,單位為秒,精度毫秒; 從讀入客戶端的第一個字節(jié)開始,直到把最后一個字符發(fā)送給客戶端后進(jìn)行日志寫入為止。

$args:這個變量等于請求行中的參數(shù),同$query_string
$content_length:請求頭中的Content-length字段。
$content_type:請求頭中的Content-Type字段。
$document_root:當(dāng)前請求在root指令中指定的值。
$host:請求主機頭字段,否則為服務(wù)器名稱。
$http_user_agent:客戶端agent信息
$http_cookie:客戶端cookie信息
$limit_rate:這個變量可以限制連接速率。
$request_method:客戶端請求的動作,通常為GET或POST。
$remote_addr:客戶端的IP地址。
$remote_port:客戶端的端口。
$remote_user:已經(jīng)經(jīng)過Auth Basic Module驗證的用戶名。
$request_filename:當(dāng)前請求的文件路徑,由root或alias指令與URI請求生成。
$scheme:HTTP方法(如http,https)。
$server_protocol:請求使用的協(xié)議,通常是HTTP/1.0或HTTP/1.1。
$server_addr:服務(wù)器地址,在完成一次系統(tǒng)調(diào)用后可以確定這個值。
$server_name:服務(wù)器名稱。
$server_port:請求到達(dá)服務(wù)器的端口號。
$request_uri:包含請求參數(shù)的原始URI,不包含主機名,如:”/foo/bar.php?arg=baz”。
$uri:不帶請求參數(shù)的當(dāng)前URI,$uri不包含主機名,如”/foo/bar.html”。
$document_uri:與$uri相同。 

[apps@ngx-ali01 conf]$ cat nginx.conf
user  apps apps;
worker_processes  auto;

error_log /apps/svr/tengine/logs/nginx_error.log error;
pid /apps/svr/tengine/logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  65535;
}


http {
      log_format main '{"@timestamp":"$time_iso8601",'
                        '"@source":"$server_addr",'
                        '"idc":"huzhou",'
                        '"http_cookie":"$http_cookie",'
                        '"hostname":"$hostname",'
                        '"ip":"$http_x_forwarded_for",'
                        '"client":"$remote_addr",'
                        '"request_method":"$request_method",'
                        '"scheme":"$scheme",'
                        '"domain":"$server_name",'
                        '"referer":"$http_referer",'
                        '"request":"$request_uri",'
                        '"args":"$args",'
                        '"size":$body_bytes_sent,'
                        '"request_body":"$request_body",'
                        '"status": $status,'
                        '"responsetime":$request_time,'
                        '"upstreamtime":"$upstream_response_time",'
                        '"upstreamaddr":"$upstream_addr",'
                        '"http_user_agent":"$http_user_agent",'
                        '"https":"$https"'
                        '}';

        access_log /apps/svr/tengine/logs/access.log main;

        limit_conn_zone $server_name zone=perserver:10m;
        limit_conn_zone $binary_remote_addr zone=perip:20m;
#       limit_req_zone $rt_filtered_ip zone=qps:10m rate=30r/s;

        include mime.types;
        default_type    application/octet-stram;
        server_tokens   off;
        server_tag      off;
        server_info     off;
        server_names_hash_bucket_size 128;
        sendfile        on;
        keepalive_timeout 65;
        gzip  on;
        gzip_http_version 1.1;
        gzip_vary on;
        gzip_comp_level 6;
        gzip_proxied any;
        gzip_types text/plain  text/css application/json  application/x-javascript text/xml application/xml application/xml+rss text/javascript application/x-shockwave-flash image/png image/x-icon image/gif image/jpeg;
        gzip_buffers 16 8k;
        proxy_temp_path /dev/shm/temp;
        proxy_cache_path /dev/shm/cache levels=2:2:2 keys_zone=cache_go:20m inactive=1d max_size=2g;

        vhost_traffic_status_zone;
        vhost_traffic_status_filter_by_host on;
        include conf.d/*.conf;
        include ztoky_cn/*.conf;
}

到此這篇關(guān)于Nginx日志輸出配置json格式的文章就介紹到這了,更多相關(guān)Nginx日志輸出json內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 教你利用Nginx 服務(wù)搭建子域環(huán)境提升二維地圖加載性能的步驟

    教你利用Nginx 服務(wù)搭建子域環(huán)境提升二維地圖加載性能的步驟

    這篇文章主要介紹了利用 Nginx 服務(wù)搭建子域環(huán)境提升二維地圖加載性能,本文分步驟通過實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-09-09
  • Nginx啟用GZIP壓縮網(wǎng)頁傳輸方法(推薦)

    Nginx啟用GZIP壓縮網(wǎng)頁傳輸方法(推薦)

    Gzip壓縮我很早已經(jīng)就啟用了,不過從未與大家分享過。今天小編給大家分享Nginx啟用GZIP壓縮網(wǎng)頁傳輸方法,需要的朋友參考下吧
    2017-01-01
  • Nginx使用自簽ssl證書實現(xiàn)https連接的方法

    Nginx使用自簽ssl證書實現(xiàn)https連接的方法

    本文主要介紹了Nginx使用自簽ssl證書實現(xiàn)https連接的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • nginx反向代理文件下載失敗問題及解決

    nginx反向代理文件下載失敗問題及解決

    這篇文章主要介紹了nginx反向代理文件下載失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Nginx中配置WebSocket代理的詳細(xì)步驟

    Nginx中配置WebSocket代理的詳細(xì)步驟

    Nginx 可以配置為 WebSocket 代理,將 WebSocket 連接從客戶端轉(zhuǎn)發(fā)到后端服務(wù)器,以下是如何在 Nginx 中配置 WebSocket 代理的詳細(xì)步驟和示例配置,需要的朋友可以參考下
    2025-02-02
  • Nginx的一些常用配置匯總

    Nginx的一些常用配置匯總

    nginx配置說簡單也簡單,說復(fù)雜也復(fù)雜,入門簡單,精通難,下面這篇文章主要給大家介紹了關(guān)于Nginx的一些常用配置,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • Nginx禁止指定UA訪問的方法

    Nginx禁止指定UA訪問的方法

    這篇文章主要介紹了Nginx禁止指定UA訪問的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Ubuntu上安裝Nginx服務(wù)器程序及簡單的環(huán)境配置小結(jié)

    Ubuntu上安裝Nginx服務(wù)器程序及簡單的環(huán)境配置小結(jié)

    Nginx是一款高性能的異步非阻塞服務(wù)器應(yīng)用程序,人氣相當(dāng)高,這里我們就來看一下在Ubuntu上安裝Nginx服務(wù)器程序及簡單的環(huán)境配置小結(jié):
    2016-07-07
  • nginx中斜杠(‘/‘)的具體使用

    nginx中斜杠(‘/‘)的具體使用

    在Nginx配置的過程中,斜杠(/)經(jīng)常使用到,它們不僅可以區(qū)分不同的路徑,還有其他的作用,本文就詳細(xì)的介紹了nginx中斜杠(‘/‘)的具體使用,感興趣的可以了解一下,感興趣的可以了解一下
    2023-10-10
  • Nginx增添api接口的實現(xiàn)方法

    Nginx增添api接口的實現(xiàn)方法

    這篇文章給大家介紹了Nginx增添api接口的方法,文章通過代碼示例介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的幫助,具有一定的參考價值,需要的朋友可以參考下
    2023-10-10

最新評論