1 【背景】
前一段时间调研了几款搭建日志分析平台的方式,再系统的操作一下温故一下。
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,
同时也提供了IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强,
事实上nginx的并发能力在同类型的网页服务器中表现较好。
下面搭建一下其日志分析显示系统。
2 【环境】
Vmware 版本16.2.3 build-19376536
Ubuntu 版本20.4.5
3 【nginx部署】
3.1 步骤
- 首先查看Ubuntu下nginx状态:
systemctl status nginx.service
之前卸载nginx时没卸载干净,导致此错误,执行以下指令清除干净后安装即可
- 卸载旧的安装:
sudo apt-get remove nginx nginx-common
sudo apt-get purge nginx nginx-common
sudo apt-get autoremove
sudo apt-get remove nginx-full nginx-common
- 重新安装:
sudo apt-get install nginx
- 启动:(两种方式均可)
sudo /etc/init.d/nginx start #通过init.d下的启动文件启动。
sudo service nginx start #通过ubuntu的服务管理器启动
- 检查启动http://localhost:
启动成功
- 配置:
路径
3.2 配置文件组成
由全局块+events块+http块组成
全局块
从配置文件开始到events之间的内容,主要会设置一些影响Nginx服务器整体运行的配置指令,主要包括配置运行Nginx服务器的用户(组)、允许生成的worker process数,进程pid存放路径、日志存放路径和类型以及配置文件的引入等。
worker_processes 1;
#这个是Nginx服务器并发处理服务的关键配置,worker_processes值越大,可以支持的并发处理量越多,但是会受到硬件、软件等设备的制约。
events块
events块设计的指令主要影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多work process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个work process可以同时支持的最大连接数等。下面的例子表示每个work process支持的最大连接数为1024。这部分配置对Nginx的性能影响较大,在实际中应该灵活配置。
events {
worker_connections 1024;
}
http块
Nginx服务器配置中最频繁的部分,代理、缓存和日志定义等绝大多数功能和第三方模块的配置都在这里,http块又包括http全局块和server块。
http全局块
http全局块配置的指令包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等。
http server块
这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。
每个http块可以包括多个server块,而每个server块就相当于一个虚拟主机。
每个server块也可以分为全局server块,以及可以同时包含多个location块。
全局server块
最常见的配置时本虚拟主机的监听配置和本虚拟主机的名称或IP配置。
location块
一个server块可以配置多个location块。
这块的主要作用是基于Nginx服务器接收到的请求字符串(例如server_name/uri-string),对虚拟主机名称(也可以是IP别名)之外的字符串(例如前面的/uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。
3.3 测试可用配置如下:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 3600;
client_header_timeout 3600;
client_body_timeout 3600;
proxy_connect_timeout 3600;
proxy_send_timeout 3600;
proxy_read_timeout 3600;
types_hash_max_size 2048;
client_max_body_size 36G;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# Mccts-start
server {
listen 2333;
server_name localhost;
root /home/mccts/start;
index index.html index.htm;
location / {
# 截取404的uri,传给 @router
try_files $uri $uri/ @router;
}
location @router {
# 接到截取的uri 并按一定规则重写uri和vue路由跳转
rewrite ^.*$ /index.html last;
}
}
# McctsControl
server {
listen 9527;
server_name localhost;
autoindex on;
root /home/mccts/mccts-control/web;
index index.html index.htm;
location / {
# 截取404的uri,传给 @route
try_files $uri $uri/ @router;
}
location /control/ {
proxy_pass http://127.0.0.1:8080/;
#proxy_pass http://192.168.1.28:8080/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /analysis/ {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /Rocket/ {
proxy_pass http://127.0.0.1:3101/Rocket/;
}
location @router {
# 接到截取的uri 并按一定规则重写uri和vue路由跳转
rewrite ^.*$ /index.html last;
}
}
# McctsAnalysis
server {
listen 9528;
server_name localhost;
root /home/mccts/mccts-analysis/web;
index index.html index.htm;
location / {
# 截取404的uri,传给 @route
try_files $uri $uri/ @router;
}
location /analysis {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# McctsObject
server {
listen 9529;
server_name localhost;
root /home/mccts/mccts-object/web;
index index.html index.htm;
location / {
# 截取404的uri,传给 @route
try_files $uri $uri/ @router;
}
location /object {
proxy_pass http://127.0.0.1:8083/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location @router {
# 接到截取的uri 并按一定规则重写uri和vue路由跳转
rewrite ^.*$ /index.html last;
}
}
# McctsBallistic
server {
listen 3101;
server_name localhost;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
if ($request_method = 'OPTIONS') {
return 200;
}
location /Rocket/ {
proxy_pass http://127.0.0.1/Rocket/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
修改版:(修改了log_format 、access_log)
为了监控:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '{'
'"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
'"connection": "$connection", ' # connection serial number
'"connection_requests": "$connection_requests", ' # number of requests made in connection
'"pid": "$pid", ' # process pid
'"request_id": "$request_id", ' # the unique request id
'"request_length": "$request_length", ' # request length (including headers and body)
'"remote_addr": "$remote_addr", ' # client IP
'"remote_user": "$remote_user", ' # client HTTP username
'"remote_port": "$remote_port", ' # client port
'"time_local": "$time_local", '
'"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
'"request": "$request", ' # full path no arguments if the request
'"request_uri": "$request_uri", ' # full path and arguments if the request
'"args": "$args", ' # args
'"status": "$status", ' # response status code
'"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
'"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
'"http_referer": "$http_referer", ' # HTTP referer
'"http_user_agent": "$http_user_agent", ' # user agent
'"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
'"http_host": "$http_host", ' # the request Host: header
'"server_name": "$server_name", ' # the name of the vhost serving the request
'"request_time": "$request_time", ' # request processing time in seconds with msec resolution
'"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
'"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
'"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
'"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
'"upstream_response_length": "$upstream_response_length", ' # upstream response length
'"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
'"ssl_protocol": "$ssl_protocol", ' # TLS protocol
'"ssl_cipher": "$ssl_cipher", ' # TLS cipher
'"scheme": "$scheme", ' # http or https
'"request_method": "$request_method", ' # request method
'"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
'"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
'"gzip_ratio": "$gzip_ratio", '
'"http_cf_ray": "$http_cf_ray"'
'}';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 3600;
client_header_timeout 3600;
client_body_timeout 3600;
proxy_connect_timeout 3600;
proxy_send_timeout 3600;
proxy_read_timeout 3600;
types_hash_max_size 2048;
client_max_body_size 36G;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
# Mccts-start
server {
listen 2333;
server_name localhost;
root /home/mccts/start;
index index.html index.htm;
location / {
# 截取404的uri,传给 @router
try_files $uri $uri/ @router;
}
location @router {
# 接到截取的uri 并按一定规则重写uri和vue路由跳转
rewrite ^.*$ /index.html last;
}
}
# McctsControl
server {
listen 9527;
server_name localhost;
autoindex on;
root /home/mccts/mccts-control/web;
index index.html index.htm;
location / {
# 截取404的uri,传给 @route
try_files $uri $uri/ @router;
}
location /control/ {
proxy_pass http://127.0.0.1:8080/;
#proxy_pass http://192.168.1.28:8080/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /analysis/ {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /Rocket/ {
proxy_pass http://127.0.0.1:3101/Rocket/;
}
location @router {
# 接到截取的uri 并按一定规则重写uri和vue路由跳转
rewrite ^.*$ /index.html last;
}
}
# McctsAnalysis
server {
listen 9528;
server_name localhost;
root /home/mccts/mccts-analysis/web;
index index.html index.htm;
location / {
# 截取404的uri,传给 @route
try_files $uri $uri/ @router;
}
location /analysis {
proxy_pass http://127.0.0.1:8081/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# McctsObject
server {
listen 9529;
server_name localhost;
root /home/mccts/mccts-object/web;
index index.html index.htm;
location / {
# 截取404的uri,传给 @route
try_files $uri $uri/ @router;
}
location /object {
proxy_pass http://127.0.0.1:8083/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location @router {
# 接到截取的uri 并按一定规则重写uri和vue路由跳转
rewrite ^.*$ /index.html last;
}
}
# McctsBallistic
server {
listen 3101;
server_name localhost;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
add_header 'Access-Control-Allow-Headers' 'Content-Type';
if ($request_method = 'OPTIONS') {
return 200;
}
location /Rocket/ {
proxy_pass http://127.0.0.1/Rocket/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
4 【loki部署】
1 【背景】里的文章里有介绍,这里就说明一下修改配置文件:
12559
很奇怪没有找到数据源
启动命令:
cd [dir]
nohup ./loki -config.file=./loki.yaml > ./loki2.log 2>&1 &
nohup ./promtail -config.file=./promtail.yaml > ./promtail2.log 2>&1 &
systemctl start grafana-server
5 【效果】
预期效果(没有数据源,还没有显示)
其他Dashboard打开显示的数据