您的位置 首页 nginx

nginx log – log_format 自定义log

nginx 自定义log

在默认情况下,Nginx 的访问日志并不会直接显示请求的内部路径,因为 Nginx 主要记录的是与外部客户端的交互信息。然而,你可以使用一些技巧来记录请求的内部路径。

在 Nginx 的配置文件中使用 log_format 指令定义一个新的日志格式,将请求的内部路径包含进去,然后在相应的 access_log 指令中引用这个自定义格式。以下是一个简单的示例:

 

http {
    # 定义一个新的日志格式包含内部路径信息
    log_format custom_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$request_filename"';

    # 应用自定义日志格式到 access_log
    server {
        access_log /var/log/nginx/access.log custom_format;
        # 其他配置...
    }
}

 

在这个示例中,$request_filename 变量用于记录请求的内部文件路径。请记住,启用此功能可能会在日志中公开服务器的文件系统路径信息,这可能带来安全风险,因此请谨慎使用并确保只有授权用户能够访问日志文件。

    log_format custom_format '[$time_local] Host="$http_host", '
                             'request="$request", '
                             'status="$status", '
                             'body_bytes_sent="$body_bytes_sent", '
                             'http_referer="$http_referer", '
                             'http_user_agent="$http_user_agent", '
                             'Forwarded="$http_forwarded", '
                             'X-Forwarded-For="$http_x_forwarded_for", '
                             'X-Forwarded-Host="$http_x_forwarded_host", '
                             'X-Forwarded-Proto="$http_x_forwarded_proto", '
                             'X-REAL-IP="$http_x_real_ip", '
                             'realip_remote_addr="$realip_remote_addr", '
                             'remote_addr="$remote_addr", '
                             'server_addr="$server_addr", '
                             'request_filename="$request_filename", '
                             'http_head="$http_head", '
                             'args="$args", '
                             'upstream_addr="$upstream_addr", ' ;

 

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章