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.0\x5C; Win64\x5C; x64\x5C) AppleWebKit/537.36 \x5C(KHTML, like Gecko\x5C) Chrome/100.0.4896.60 Safari/537.36" "-" "from: https://www.baidu.com"

完成!!!

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

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

poweredbytian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值