测试不同语言 hello world 的性能。
ab -c10 -n50000 http://localhost:8000/
Server Software: openresty/1.13.6.2
Server Hostname: localhost
Server Port: 8080
Document Path: /
Document Length: 20 bytes
Concurrency Level: 10
Time taken for tests: 1.759 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 8400000 bytes
HTML transferred: 1000000 bytes
Requests per second: 28431.00 [#/sec] (mean)
Time per request: 0.352 [ms] (mean)
Time per request: 0.035 [ms] (mean, across all concurrent requests)
Transfer rate: 4664.46 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 0 0 0.1 0 2
Waiting: 0 0 0.0 0 2
Total: 0 0 0.1 0 2
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 1
99% 1
100% 2 (longest request)
Server Software:
Server Hostname: localhost
Server Port: 8000
Document Path: /
Document Length: 12 bytes
Concurrency Level: 10
Time taken for tests: 2.168 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 6450000 bytes
HTML transferred: 600000 bytes
Requests per second: 23066.21 [#/sec] (mean)
Time per request: 0.434 [ms] (mean)
Time per request: 0.043 [ms] (mean, across all concurrent requests)
Transfer rate: 2905.80 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 1
Processing: 0 0 0.0 0 2
Waiting: 0 0 0.0 0 2
Total: 0 0 0.1 0 2
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 1
98% 1
99% 1
100% 2 (longest request)
Server Software: openresty/1.13.6.2
Server Hostname: localhost
Server Port: 8080
Document Path: /server1
Document Length: 12 bytes
Concurrency Level: 10
Time taken for tests: 3.867 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 8800000 bytes
HTML transferred: 600000 bytes
Requests per second: 12930.01 [#/sec] (mean)
Time per request: 0.773 [ms] (mean)
Time per request: 0.077 [ms] (mean, across all concurrent requests)
Transfer rate: 2222.35 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 1 0.1 1 2
Waiting: 0 1 0.1 1 2
Total: 0 1 0.1 1 2
Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 1
99% 1
100% 2 (longest request)
Server Software: Werkzeug/0.14.1
Server Hostname: localhost
Server Port: 5000
Document Path: /
Document Length: 12 bytes
Concurrency Level: 10
Time taken for tests: 35.822 seconds
Complete requests: 50000
Failed requests: 0
Total transferred: 8300000 bytes
HTML transferred: 600000 bytes
Requests per second: 1395.79 [#/sec] (mean)
Time per request: 7.164 [ms] (mean)
Time per request: 0.716 [ms] (mean, across all concurrent requests)
Transfer rate: 226.27 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 2 7 1.8 7 19
Waiting: 1 7 1.7 6 17
Total: 2 7 1.9 7 19
Percentage of the requests served within a certain time (ms)
50% 7
66% 8
75% 8
80% 9
90% 10
95% 11
98% 12
99% 12
100% 19 (longest request)
https://github.com/openresty/openresty
https://github.com/moonbingbing/openresty-best-practices
https://www.nginx.com/resources/wiki/modules/index.html
http://blog.codinglabs.org/articles/intro-of-nginx-module-development.html
https://github.com/wujunze/nginx-http-echo-module
http://www.evanmiller.org/nginx-modules-guide.html
https://nginxconfig.io/
https://github.com/allinurl/goaccess
$ echo "deb http://deb.goaccess.io/ $(lsb_release -cs) main" | sudo tee -a /etc/apt/sources.list.d/goaccess.list
$ wget -O - https://deb.goaccess.io/gnugpg.key | sudo apt-key add -
$ sudo apt-get update
$ sudo apt-get install goaccess
goaccess access.log -c
goaccess access.log -o report.html --log-format=COMBINED
goaccess access.log -o /var/www/html/report.html --log-format=COMBINED --real-time-html
goaccess /home/huangjian/local/nginx/logs/access.log -o /home/huangjian/local/nginx/www/html/report.html --log-format=COMBINED --real-time-html --addr=localhost --port=7890
在 nginx.conf 中,增加以下内容:
server {
listen 7070;
server_name localhost;
location /report.html {
alias /home/huangjian/local/nginx/www/html/;
index report.html;
}
}
打开浏览器访问:localhost:7070/report.html
如果是在服务器上,可以用 ssh 把端口 7070 映射到本地电脑。
root 的处理结果是:root 路径 + location 路径
alias 的处理结果是:使用 alias 路径替换 location 路径
还有一个重要的区别是 alias 后面必须要用 “/” 结束,否则会找不到文件的,而 root 则可有可无。
location ^~ /a/ {
root /home/huangjian/local/nginx/www/html;
}
如果一个请求的 URI 是 /a/index.html 时,web 服务器将会返回服务器上的 /home/huangjian/local/nginx/www/html/a/index.html 的文件。
location ^~ /b/ {
alias /home/huangjian/local/nginx/www/html/alias_dir/;
}
如果一个请求的 URI 是 /b/index.html 时,web 服务器将会返回服务器上的 /home/huangjian/local/nginx/www/html/alias_dir/index.html 的文件。
#!/bin/bash
#Rotate the Nginx logs
logs_path=/home/huangjian/local/openresty/nginx/logs #Nginx 的日志目录
backup_path=${logs_path}/history #Nginx 日志的备份目录
expire_backup_delete="ON" #是否开启过期备份删除 ON为开启 OFF为关闭
expire_days=3 #过期时间天数 默认为三天,此项只有在expire_backup_delete开启时有效
if [ ! -d ${backup_path} ]; then
mkdir -p ${backup_path}
fi
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${logs_path}/access.log ${backup_path}/access_${YESTERDAY}.log
mv ${logs_path}/error.log ${backup_path}/error_${YESTERDAY}.log
# send signal USR1 to Nigix master process, to reopen logs.
kill -USR1 $(cat ${logs_path}/nginx.pid)
if [ "$expire_backup_delete" == "ON" -a "$backup_path" != "" ]; then
`find $backup_path/ -type d -mtime +$expire_days | xargs rm -rf`
fi
logs_path 目录和 backup_path 目录需要在同一个硬盘下。因为 mv 的时候,只有在同一个硬盘下,log 文件的 inode 才不会改变,这样日志才不会丢失。