使用log_format为Nginx服务器设置更详细的日志格式方法

yizhihongxing

使用log_format为Nginx服务器设置更详细的日志格式可以帮助我们更好地监控和分析访问日志。下面是设置更详细的日志格式的完整攻略:

步骤一:备份Nginx配置文件

在进行任何更改之前,请确保备份您的Nginx配置文件。以Ubuntu 18.04为例,可以使用以下命令备份配置文件:

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

步骤二:打开Nginx配置文件

使用文本编辑器打开Nginx配置文件,例如在Ubuntu 18.04中,可以使用如下命令打开Nginx配置文件:

sudo nano /etc/nginx/nginx.conf

步骤三:配置log_format

在http块中,使用log_format设置更详细的日志格式,例如:

http {
    log_format  acc_combined  '$remote_addr - $remote_user [$time_local] "$request" '
                            '$status $body_bytes_sent "$http_referer" '
                            '"$http_user_agent" "$http_x_forwarded_for"';
}

上面的log_format将在访问日志中包含远程IP地址,远程用户身份,访问时间,请求URI,HTTP响应状态码,响应体大小,Referer,User-Agent和X-Forwarded-For等信息。

步骤四:启用log_format

在server块中使用access_log指令启用log_format,例如:

server {
    listen 80;
    server_name example.com;
    access_log /var/log/nginx/access.log acc_combined;
    ...
}

上述access_log指令中的acc_combined对应的就是上面所定义的log_format。当然,如果不想使用自定义的log_format,可以直接使用预设的响应式Access Log,例如:

access_log /var/log/nginx/access.log;

步骤五:重新加载Nginx配置文件

保存Nginx配置文件后,使用以下命令重新加载配置文件:

sudo systemctl reload nginx

设置完成后,每当有客户端访问服务器时,做出的响应都将被记录到Nginx日志文件中,该日志文件的位置为上述设置access_log指令中的文件路径。

示例一:nginx.conf配置文件中的log_format设置

http {
    ##日志格式化 
    log_format  webservers  '$remote_addr $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" $gzip_ratio $request_time';
}

示例二:server块配置文件中的access_log指令

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;

        ##将web1日志单独写入一个文件 
        access_log /data/logs/nginx/access.log webservers;

        location /nginx_status {
          vhost_traffic_status_display;
          vhost_traffic_status_display_format html;
        }
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
}

以上两个示例来自Nginx官网,第一个示例是在http块中设置log_format,第二个示例在server块中使用access_log指令启用log_format。其中示例一中的log_format名称为webservers,涵盖了请求客户端IP地址,远程用户身份,访问时间,请求的URI,HTTP响应状态码,响应体大小,Referer,User-Agent,压缩比率和请求处理时间等信息。示例二中使用access_log指令将log_format记录到了access.log文件中。在这个例子中,使用的log_format是webservers,与示例一中的log_format名称保持一致。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:使用log_format为Nginx服务器设置更详细的日志格式方法 - Python技术站

(0)
上一篇 2023年5月16日
下一篇 2023年5月16日

相关文章

  • nginx 无法解析php unix:/tmp/php-cgi.sock

    首先先安装php-fpm, yum install php-fpm     查看 php-fpm.conf   [www] listen = /tmp/php-cgi.sock   nginx.conf server{ location ~ \.php{fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index in…

    Nginx 2023年4月11日
    00
  • nginx监听端口转发到后端改变的问题

    nginx配置: server {     listen 80;     server_name localhost;     location / {         proxy_pass http://192.168.1.100:8080;         proxy_set_header Host $host;         proxy_set_he…

    Nginx 2023年4月10日
    00
  • Nginx + PHP CGI的fix_pathinfo安全漏洞

    具体的重现过程,用php代码修改后缀名后上传,比如说http://www.xx.com/1.jpg,访问的时候用http://www.xx.com/1.jpg/xxx.php http://docs.php.net/manual/zh/ini.core.phpcgi.fix_pathinfo “1″ PHP_INI_ALL 从 PHP 4.3.0 起可用 请…

    Nginx 2023年4月13日
    00
  • 用JDK生成Nginx可用的https自签名证书及常用配置

    测试环境:Windows10、nginx-1.14.0步骤 一、生成证书 这里使用自己生成的免费证书。在${JAVA_HOME}/bin 下可以看到keytool.exe,在改目录打开cmd然后输入: keytool -genkey -v -alias tomcat -keyalg RSA -keystore d:\local.keystore -valid…

    Nginx 2023年4月13日
    00
  • 详解Nginx反向代理跨域基本配置与常见误区

    下面我来详细讲解一下“详解Nginx反向代理跨域基本配置与常见误区”的完整攻略,包含了两条示例说明。 理解跨域问题 首先,需要了解什么是跨域问题。在Web开发中,是禁止浏览器向不同域名或者不同端口发送请求的。这是为了保证安全,防止恶意网站向其他网站获取数据。但在某些情况下,我们需要在不同的域名或者端口之间来传递数据,这时候就需要使用跨域技术。常用的跨域技术有…

    Nginx 2023年5月16日
    00
  • nginx访问日志获取访问前10的url

    在ELK里面获取top10的url在日志量非常大的情况下是非常消耗内存的,所以写了一个脚本用来快速获取。 配置文件 log.conf [log] log_file = /data/logs/nginx/access_all.log [id_rsa] id_rsa = /root/.ssh/id_rsa 程序log.py #!/usr/bin/env pyth…

    Nginx 2023年4月10日
    00
  • Nginx负载均衡配置简单配置方法

    可以按照以下步骤来配置Nginx负载均衡: 1.安装Nginx,以Ubuntu为例: sudo apt update sudo apt install nginx 2.调整配置文件 通过修改Nginx的配置文件 /etc/nginx/nginx.conf 来实现负载均衡。使用编辑器(例如vi),打开 /etc/nginx/nginx.conf 文件进行编辑:…

    Nginx 2023年5月16日
    00
  • Nginx学习之静态文件服务器配置方法

    Nginx学习之静态文件服务器配置方法 概述 在Web开发中,静态文件服务器(Static Files Server)是指能够有效且迅速地处理静态文件的服务器。通过静态文件服务器,我们可以快速地响应静态资源文件的请求,如CSS、JavaScript、图片等。 Nginx是一个高性能、高并发、高稳定性的Web服务器,常被用于静态资源文件的服务配置。本文主要介绍…

    Nginx 2023年5月16日
    00
合作推广
合作推广
分享本页
返回顶部