nginx 的access log 指定格式设置

百度已经开始收录自己的网站的情况下,如何记录从百度过来的链接。

几种解决方案:

1 在自己在程序里面纪录 http_referer ,这个需要写程序,但是业务实际还没有统计,暂时不需要在数据库中记录来源。

2 在nginx的access中纪录。

今天采用了第二种方式。

首先确定 nginx的log_format 在哪里设置的,在nginx的配置文件中的 http配置模块

ser  nginx;
worker_processes  auto;

pid        /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
multi_accept on;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    charset UTF-8;
    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay on;
    error_log /var/log/nginx/error.log error;
    server_tokens  off;
    client_max_body_size 0;
    keepalive_timeout 120s;
    client_header_timeout 120s;
    client_body_timeout 120s;  
    reset_timedout_connection on; 
    send_timeout 10; 
    limit_conn_zone $binary_remote_addr zone=addr:5m; 
    limit_conn addr 100; 
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 256k;
    gzip on;
    gzip_disable   "MSIE [1-6].";
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 4;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '  
               '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" "from: $http_referer"';

###################### Vhost ################################
    include /etc/nginx/conf.d/*.conf;
}

增加 log_format main

log_format main '$remote_addr - $remote_user [$time_local] "$request" '  
                '$status $body_bytes_sent "$http_referer" '  
                '"$http_user_agent" "$http_x_forwarded_for"';

然后配置 具体的  a.conf 中的 access日志输出

 access_log  /var/log/nginx/aa.com-access.log main;

然后重启nginx 使其生效。

效果如下:

35.171.144.2 - - [18/Jan/2024:12:08:43 +0800] "GET / HTTP/1.1" 200 12759 "https://www.baidu.com" "Mozilla/5.0 x5C(Windows NT 10.0x5C; Win64x5C; x64x5C) AppleWebKit/537.36 x5C(KHTML, like Geckox5C) Chrome/100.0.4896.60 Safari/537.36" "-" "from: https://www.baidu.com"

完成!!!

又是碰到问题解决问题,实现自我价值的一天。