[TOC]

nginx的日志格式

  • nginx访问日志

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';
    # 将容器运行的输出内容存放在文件中
    (docker run -it busybox free -m) > /opt/nginx/mem.info

    # 查看输出信息
    cat /opt/nginx/mem.info
    total used free shared buff/cache available
    Mem: 973 315 94 0 563 498
    Swap: 1024 49 975


    作者: Chenlin
    链接: https://www.chenlin.cloud/archives/2f86fa8c.html
    来源: 奥利奥の麦旋风
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • nginx的日志格式解析

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    $remote_addr			# 记录客户端IP地址
    $remote_user # 记录客户端用户名
    $time_local # 记录通用的本地时间
    $time_iso8601 # 记录ISO8601标准格式下的本地时间
    $request # 记录请求的方法以及请求的http协议
    $status # 记录请求状态码(用于定位错误信息)
    $body_bytes_sent # 发送给客户端的资源字节数,不包括响应头的大小
    $bytes_sent # 发送给客户端的总字节数
    $msec # 日志写入时间。单位为秒,精度是毫秒。
    $http_referer # 记录从哪个页面链接访问过来的
    $http_user_agent # 记录客户端浏览器相关信息
    $http_x_forwarded_for # 记录客户端IP地址
    $request_length # 请求的长度(包括请求行, 请求头和请求正文)。
    $request_time # 请求花费的时间,单位为秒,精度毫秒

    # 注:如果Nginx位于负载均衡器,nginx反向代理之后, web服务器无法直接获取到客户端真实的IP地址
    # $remote_addr获取的是反向代理的IP地址。 反向代理服务器在转发请求的http头信息中
    # 增加X-Forwarded-For信息,用来记录客户端IP地址和客户端请求的服务器地址

nginx日志切割

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 日志截取
cat /etc/logrotate.d/nginx

# 日志配置信息解析
/var/log/nginx/*.log {
daily # 每天切割
missingok # 忽略错误日志
rotate 52 # 保存52天的日志
compress # 压缩
delaycompress # 延迟压缩日志
notifempty # 空文件不切割
create 640 nginx adm # 日志文件的权限 属主及属组
sharedscripts # 执行脚本
postrotate # 重新加载nginx配置文件
if [ -f /var/run/nginx.pid ]; then
kill -USR1 `cat /var/run/nginx.pid`
fi
endscript
}

nginx虚拟主机

三种虚拟主机配置方式

Nginx配置虚拟主机有如下三种方式:

  • 主机多IP方式
  • 基于端口的配置方式
  • 基于多个hosts名称方式(多域名方式)

主机多IP方式

image-20230616191837973

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
42
43
44
45
46
47
48
49
50
# 新建虚拟命令
ip addr add IP/子网掩码 dev eth0

# 新建网卡
ip addr add 10.0.0.11/24 dev eth0
ip addr add 10.0.0.12/24 dev eth0

# 查看网卡信息
ip a

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group
default qlen 1000
link/ether 00:0c:29:c8:00:9a brd ff:ff:ff:ff:ff:ff
inet 10.0.0.9/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 10.0.0.11/24 scope global secondary eth0
valid_lft forever preferred_lft forever
inet 10.0.0.12/24 scope global secondary eth0
valid_lft forever preferred_lft forever

# 修改第一个网页配置信息
vim /etc/nginx/conf.d/game.conf
server {
listen 80;
server_name 10.0.0.11;

location / {
root /code/h5_games;
index index.html;
}
}

# 修改第二个网页配置信息
vim /etc/nginx/conf.d/xxx.conf
server {
listen 80;
server_name 10.0.0.12;

location / {
root /code;
index index.html;
}
}

# 重启nginx
systemctl restart nginx

# 访问浏览器
10.0.0.11
10.0.0.12

基于多端口

image-20230616192118336

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
# 修改第一个网页配置信息
server {
listen 8081;
server_name 10.0.0.9;

location / {
root /code;
index index.html;
}
}

# 修改第二个网页配置信息
server {
listen 8080;
server_name 10.0.0.9;

location / {
root /code/h5_games;
index index.html;
}
}

# 浏览器访问
10.0.0.9:8080
10.0.0.9:8081

多域名解析

image-20230616192210685

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 修改网页配置信息
server {
listen 80;
server_name yyy.xxx.com;

location / {
root /code;
index index.html;
}
}

# 修改网页配置信息
server {
listen 80;
server_name xxx.game.com;

location / {
root /code/h5_games;
index index.html;
}
}

# 域名解析

image-20221212155910978

1
2
3
# 访问域名
yyy.xxx.com
xxx.game.com