大网数据广告活动
网站运营与故障问答

Generating Nginx log file entries for logrotate testing

· 52CE 我爱测

How can I generate Nginx log file data for one or more test (burner) domains on a test server? I want to essentially mimic a live website with regular traffic being logged but without the provision of a legit website. I have star…

问题原文

原帖链接 · Pete Cooper · CC BY-SA 4.0

How can I generate Nginx log file data for one or more test (burner) domains on a test server? I want to essentially mimic a live website with regular traffic being logged but without the provision of a legit website.

I have started learning about logrotate and I am adapting it for the sites on my servers. In order to more fully understand how it works, I am ideally looking to replicate log file entries as if the test domains are live sites, without opening the domains up to a larger audience since they're essentially disposable domains for this purpose.

I have full (root) access to my VPS, I have Nginx set up as I want it, the test domains are all web-side rather than internal, and I have log files that currently just get bigger as time progresses…hence the need for logrotate.

I do not need a benchmarking, concurrency or load testing tool in the traditional sense as I don't want to overwhelm the server, so something like ab or siege that can run in drip-drip-drip mode might be a route to investigate.

已采纳解答

原帖链接 · Aaron · CC BY-SA 4.0

You could certainly use something like ab or siege -- but it seems like a lot of work for little benefit, at least given what you're trying to accomplish.

If I were you I'd just create some random/empty files. Give them the right name and put them in the right place and logrotate should do its job normally -- it doesn't care at all about what the contents of the files are, which makes sense since log files may look different for different services or even for different users of the same service.

At its simplest, something like this might be enough for you:

touch /var/log/nginx/access.log

Or if you wanted to get a tiny bit fancier you append the date/time to your log file so you could track when the logs are actually getting rotated. You could put something like this to your crontab, having it run every minute/hour/whatever:

echo `date` >> /var/log/nginx/access.log

I suppose that if you wanted to have your fake logs look more like real logs you could do something like this:

# Generate Random IP Address
IP_ADDRESS="$(( $RANDOM % 254 + 1 )).$(( $RANDOM % 254 + 1 )).$(( $RANDOM % 254 + 1 )).$(( $RANDOM % 254 + 1 ))"
# Get the current date/time
NOW=$(date +"%d/%b/%Y:%H:%M:%S")
# Append a fake entry to the log file
echo "$IP_ADDRESS - - [$NOW +0000] \"GET / HTTP/2.0\" 200 5316 \"https://example.com/\" \"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36\" \"-\"" >> /var/log/nginx/access.log

The above will create lines that look like this, using a randomly generated IP address and the current date/time:

79.139.188.5 - - [23/Sep/2020:16:07:54 +0000] "GET / HTTP/2.0" 200 5316 "https://example.com/" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "-"

However, making your logs look like real logs seems like overkill to me, and to my way of thinking it will just make debugging more complicated than just appending the date/time to the file.

All that said, if you're rotating logs based on file size, you could use something like the truncate command to generate files of various sizes. For instance, this command creates a 4MB file:

truncate -s 4M /var/log/nginx/access.log

Clearly none of these options actually simulates Nginx logs, but again, that's not really necessary to test logrotate. Finally, you might also find it helpful to use the --force option for logrotate so that you can see immediately how your logs will be rotated. (See: https://www.shellhacks.com/logrotate-force-log-rotation/).

Good luck!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2020-09-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。