第二弹,跟随陶辉老师的《Nginx核心知识100讲》学习使用nginx搭建一个可用的静态资源Web服务器和使用GoAccess实现日志可视化。
启动Nginx
1 2 3 4 5 6
| cd nginx ./sbin/nginx ps -ef | grep nginx root 8344 1 0 20:07 ? 00:00:00 nginx: master process ./sbin/nginx nobody 8345 8344 0 20:07 ? 00:00:00 nginx: worker process root 8849 8267 0 20:16 pts/1 00:00:00 grep --color=auto nginx
|
静态资源
陶辉老师在课中使用了Dlib的docs作为静态资源,直接下载解压后,将docs目录拷贝至nginx目录中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| # ll 总用量 56 drwxr-xr-x 12 top top 4096 3月 17 19:57 ./ drwxr-xr-x 15 top top 4096 3月 17 19:59 ../ drwx------ 2 nobody top 4096 3月 17 19:50 client_body_temp/ drwxr-xr-x 2 top top 4096 3月 17 20:07 conf/ drwxr-xr-x 10 top top 12288 3月 17 19:57 dlib/ drwx------ 2 nobody top 4096 3月 17 19:50 fastcgi_temp/ drwxr-xr-x 2 top top 4096 3月 17 19:50 html/ drwxr-xr-x 2 top top 4096 3月 17 20:07 logs/ drwx------ 2 nobody top 4096 3月 17 19:50 proxy_temp/ drwxr-xr-x 2 top top 4096 3月 17 19:50 sbin/ drwx------ 2 nobody top 4096 3月 17 19:50 scgi_temp/ drwx------ 2 nobody top 4096 3月 17 19:50 uwsgi_temp/
|
配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| http { include mime.types;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
gzip on; # 小于1字节的就不压缩 gzip_min_length 1; # 压缩级别,针对某些类型做gzip压缩 gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
server { listen 8080; server_name localhost;
#设置日志位置和格式 access_log logs/host.access.log main;
location / { alias dlib/; # 提供访问以/结尾的url时,显示目录的结构 autoindex on; # 限制每秒传输的字节数,开启该选项后访问会变慢 # set $limit_rate 1k; }
# redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
|
实现效果
访问首页的效果
设置gzip后,观察控制台Network,可以看到相关文件的Content-Encoding
已经是gzip了,比如:
1 2 3 4 5 6 7 8 9 10 11
| General Request URL: http://localhost:8080/dlib-logo.png Request Method: GET Status Code: 200 OK Remote Address: 127.0.0.1:8080 Referrer Policy: no-referrer-when-downgrade Response Headers Connection: keep-alive Content-Encoding: gzip Content-Type: image/png Date: Sun, 17 Mar 2019 12:31:03 GMT
|
访问dlib目录结构的效果
查看log文件:
1 2 3 4 5 6 7
| # cat host.access.log 127.0.0.1 - - [17/Mar/2019:20:42:17 +0800] "GET / HTTP/1.1" 200 7600 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-" 127.0.0.1 - - [17/Mar/2019:20:42:17 +0800] "GET /dlib.js HTTP/1.1" 200 2240 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-" 127.0.0.1 - - [17/Mar/2019:20:42:17 +0800] "GET /dlib.css HTTP/1.1" 200 1875 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-" 127.0.0.1 - - [17/Mar/2019:20:42:17 +0800] "GET /dlib-logo.png HTTP/1.1" 200 5742 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-" 127.0.0.1 - - [17/Mar/2019:20:42:17 +0800] "GET /plus.gif HTTP/1.1" 200 88 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-" 127.0.0.1 - - [17/Mar/2019:20:42:17 +0800] "GET /dlib-icon.ico HTTP/1.1" 200 1150 "http://localhost:8080/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36" "-"
|
日志可视化
使用GoAccess进行日志可视化
安装配置
在官方文档中提到了安装方法,操作如下:
1 2 3 4
| $ 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
|
在nginx.conf
中添加如下配置:
1 2 3
| location /report.html { alias /home/top/nginx/html/report.html; }
|
启动GoAccess
指定日志位置,输入文件,采用实时更新,设置时间、日期、日志的格式:
1
| $ goaccess /home/top/nginx/logs/host.access.log -o /home/top/nginx/html/report.html --time-format='%H:%M:%S' --date-format='%d/%b/%Y' --log-format=COMBINED --real-time-html --daemonize
|
注: 其中--daemonize
使GoAccess作为守护程序运行(仅在--real-time-html
开启下有效)。
此时去访问http://localhost:8080/report.html
,可以看到实现效果:
好了,搭建一个基础的静态资源Web服务器和日志可视化就完事啦,用起来真的是超级方便。