如果使用了 Cloudflare, 就可以给日志加一个 IP 归属地。

Cloudflare 发送到服务器的请求中,header 带一个字段 CF-IPCountry ,它显示了客户端 IP 归属地。
在 Nginx.conf http 里面自定义一个日志格式,叫 with_ip_location 。

log_format with_ip_location '$http_CF_IPCountry $http_CF_Connecting_IP [$time_local] $request $status $body_bytes_sent $http_referer $heep_user_agent';

然后指定使用这个格式。

access_log /home/wwwlogs/blog.log with_ip_location;

日志就可以显示 IP 归属地了。

US 66.249.73.131 [24/Apr/2024:14:28:36 +0800] GET /test HTTP/1.1 404 2265 - Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.94 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)