Nginx(1)基础部分

Nginx基础概述

nginx简述
Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。
开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

我们为什么选择Nginx服务
Nginx非常轻量
功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
代码模块化 (易读,便于二次开发,对于开发人员非常友好)

互联网公司都选择Nginx
1.Nginx技术成熟,具备的功能是企业最常使用而且最需要的
2.适合当前主流架构趋势, 微服务、云架构、中间层
3.统一技术栈, 降低维护成本*,* 降低技术更新成本。

Nginx采用Epool网络模型,Apache采用Select模型
Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。

Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。
处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。用一个比喻来解释Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:

假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之 一,select和epoll的性能谁的性能更高,同样十分明了。

Nginx典型应用场景
在这里插入图片描述

Nginx和Apache区别

Nginx

轻量级,采用 C 进行编写,同样的 web 服务,会占用更少的内存及资源

抗并发,nginx 以 epoll and kqueue 作为开发模型,处理请求是异步非阻塞的,负载能力比 apache 高很多,而 apache 则是阻塞型的。在高并发下 nginx 能保持低资源低消耗高性能 ,而 apache 在 PHP 处理慢或者前端压力很大的情况下,很容易出现进程数飙升,从而拒绝服务的现象。

nginx 处理静态文件好,静态处理性能比 apache 高三倍以上

nginx 的设计高度模块化,编写模块相对简单

nginx 配置简洁,正则配置让很多事情变得简单,而且改完配置能使用 -t 测试配置有没有问题,apache 配置复杂 ,重启的时候发现配置出错了,会很崩溃

nginx 作为负载均衡服务器,支持 7 层负载均衡

nginx 本身就是一个反向代理服务器,而且可以作为非常优秀的邮件代理服务器

启动特别容易, 并且几乎可以做到 7*24 不间断运行,即使运行数个月也不需要重新启动,还能够不间断服务的情况下进行软件版本的升级

社区活跃,各种高性能模块出品迅速

Apache
apache 的 rewrite 比 nginx 强大,在 rewrite 频繁的情况下,用 apache

apache 发展到现在,模块超多,基本想到的都可以找到

apache 更为成熟,少 bug ,nginx 的 bug 相对较多

apache 超稳定

apache 对 PHP 支持比较简单,nginx 需要配合其他后端用

apache 在处理动态请求有优势,nginx 在这方面是鸡肋,一般动态请求要 apache 去做,nginx 适合静态和反向。

常用的服务器软件

静态WEB软件:
          nginx
          apache
          IIS
          lighttpd
          tengine
          openresty-nginx

动态WEB软件:
          Tomcat
          Resin
          weblogic
          Jboss

Nginx的安装方式

1.源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐 4.规范 5.自由选择功能 6.选择安装位置 7.便于管理)

2.epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)

3.官方仓库=>Nginx (1.版本较新稳定版 2.安装简单 3.配置易读)

通过官网YUM仓库进行安装

配置Nginx仓库 找到nginx官网仓库配置文档
https://nginx.org/en/linux_packages.html#RHEL-CentOS

配置官方yum源

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo 
[nginx-stable]								# 名称
name=nginx stable repo						# 名称
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/		# 仓库的URL
gpgcheck=1															# MD5校验
enabled=1															# 1为开启仓库 0为关闭仓库
gpgkey=https://nginx.org/keys/nginx_signing.key						# MD5指纹

安装Nginx

[root@web01 ~]# yum -y install nginx

启动Nginx加入开机自动运行

[root@web01 ~]# systemctl start nginx
[root@web01 ~]# systemctl enable nginx

检查端口和进程 Nginx默认端口80

[root@web01 ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22138/nginx: master

查看进程:

[root@web01 ~]# ps axu|grep nginx
root      22138  0.0  0.0  49012  1004 ?        Ss   09:36   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf

查看Nginx版本号:

[root@web01 ~]# nginx -v
nginx version: nginx/1.22.0

查看Nignx的编译参数:

[root@web01 ~]# nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

浏览器访问10.0.0.7
在这里插入图片描述访问的是Nginx默认页面

Nginx服务动作:

通过systemctl来管理Nginx服务

[root@web01 ~]#systemctl start nginx   # 启动Nginx服务
[root@web01 ~]#systemctl stop nginx    # 停止Nginx服务
[root@web01 ~]#systemctl restart ngixn # 重启Nginx服务
[root@web01 ~]#systemctl reload nginx  # 重新加载nginx服务配置
[root@web01 ~]#systemctl status nginx  # 查看Nginx服务状态
[root@web01 ~]#systemctl enable nginx  # 开机自动启动

不使用systemctl管理nginx服务 使用nginx的命令直接运行服务

[root@web01 ~]# /usr/sbin/nginx		      # 启动Nginx服务   
[root@web01 ~]# /usr/sbin/nginx -s stop	  # 停止Nginx服务
[root@web01 ~]# /usr/sbin/nginx -s reload # 重新加载Nginx服务

报错:

[root@web01 ~]# /usr/sbin/nginx -s reload
nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)

nginx: 错误 打开 /var/run/nginx.pid 失败 找不到这个文件
    当服务运行后都会产生PID号
    nginx产生的PID号会记录到/var/run/nginx.pid 文件中

强制杀死Nginx进程:
先过滤进程ID

[root@web01 ~]# ps axu|grep nginx
root      22229  0.0  0.0  49012  1000 ?        Ss   09:53   0:00 nginx: master process /usr/sbin/nginx -c etc/nginx/nginx.conf
nginx     22230  0.0  0.1  49416  1972 ?        S    09:53   0:00 nginx: worker process
root      22238  0.0  0.0 112704   960 pts/1    R+   09:54   0:00 grep --color=auto nginx

使用kill命令强制杀死

[root@web01 ~]# kill -9 22229
[root@web01 nginx]# /usr/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) # 端口被占用 或者 服务已经启动

Nginx相关文件介绍

为了让大家更清晰的了解Nginx软件的全貌,可使用rpm -ql nginx查看整体的目录结构及对应的功能,如下表格整理了Nginx比较重要的配置文件

1.Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf配置文件nginx主配置文件
/etc/nginx/conf.d/default.conf配置文件默认网站配置文件

2.Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params配置文件Fastcgi代理配置文件
/etc/nginx/scgi_params配置文件scgi代理配置文件
/etc/nginx/uwsgi_params配置文件uwsgi代理配置文件

3.Nginx编码相关配置文件

路径类型作用
etc/nginx/win-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-win配置文件Nginx编码转换映射文件
/etc/nginx/mime.types配置文件Content-Type与扩展名

4.Nginx管理相关命令

路径类型作用
/usr/sbin/nginx命令Nginx命令行管理终端工具
/usr/sbin/nginx-debug命令Nginx命令行与终端调试工具

5.Nginx日志相关目录与文件

路径类型作用
/var/log/nginx目录Nginx默认存放日志目录
/etc/logrotate.d/nginx配置文件Nginx默认的日志切割

Nginx默认配置文件

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

主配置文件
nginx主配置文件是用来连接客户端和响应客户端的参数

[root@web01 nginx]# cat nginx.conf 

----------------------------------------核心区块
user  nginx;					# 启动Nginx使用的用户 安装nginx自动创建nginx虚拟用户 不需要自己创建
worker_processes  auto;		    # 进程数量 自动的和CPU核心数量一致

error_log  /var/log/nginx/error.log notice;  # 错误日志的位置
pid        /var/run/nginx.pid;				 # 存放PID的文件位置

-----------------------------------------事件模块

events {
    worker_connections  25535;				 # 每个进程最大的连接数
	use epoll;      						 # Nginx默认使用的epoll模型 可以不写
}
-----------------------------------------http区块主要响应客户端连接

http {
    include       /etc/nginx/mime.types;	  # 媒体类型.mp4 .avi .txt .doc
    default_type  application/octet-stream;   # 如果没有支持的类型 默认为下载

	#日志格式
    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; # 调用main变量格式

    sendfile        on;						    # 高效传输
    #tcp_nopush     on;

    keepalive_timeout  65;						# 长连接超时时间

    #gzip  on;									# 是否压缩

    include /etc/nginx/conf.d/*.conf;			# 将conf.d目录下所有的*.conf 包含到nginx.conf主配置文件
}

include说明:

vim /etc/nginx/nginx.conf
http {
log main  日志格式
send_file 高效传输
......
......
	server {
		listen 80
		server_name www.linuxnc.com
		cat test.txt # test.txt绝对路径是/etc/nginx/test.txt
	}

}

--------------------------------上面写法和下面的写法相同--------------------------------
下面的写法通过包含关系将配置文件分开 更易读

vim /etc/nginx/nginx.conf
http {
log main  日志格式
send_file 高效传输
......
......
include /etc/nginx/conf.d/*.conf
} 
 
 
vim /etc/nginx/conf.d/www.conf
server {
	listen 80
	server_name www.linuxnc.com
	cat test.txt # test.txt绝对路径是: /etc/nginx/test.txt
}

nginx的server配置文件
nginx的server区块用来给用户返回真实数据的配置文件
用户访问的什么 server区块就需要返回什么内容

[root@web01 conf.d]# cat default.conf 
server {
	listen 80;		 			  # 监听的端口 服务默认运行在哪个端口 类似SSH的22端口
	server_name www.ahui.com;   # 网站域名 如果写域名需要做hosts解析 空的直接使用IP地址来访问
								  # 配置为域名也可以用IP地址来访问

	location / {				  # 匹配访问的路径
	root /code/;				  # 用户访问网站数据 默认在/code目录下找
	index index.html index.htm;   # 默认给用户返回Index.html 或者返回index.htm
	}

}

http server location扩展了解项
http{}层下允许有多个**Server{}层,一个Server{}层下又允许有多个**Location
http{} 标签主要用来解决用户的请求与响应。
server{} 标签主要用来响应具体的某一个网站。
location{} 标签主要用于匹配网站具体URL路径。

日志格式

[root@web01 ~]#tail /var/log/nginx/access.log 
10.0.0.1 - - [15/Jun/2022:12:25:13 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://www.b13king.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36 Edg/102.0.1245.39" "-"

$remote_addr # 来源公网IP地址 谁访问的我
$remote_user # 远程登陆用户
[$time_local] # 本地时间
$request # 请求的链接URL
$status # 状态码
$body_bytes_sent # 服务端给客户端返回数据的大小
$http_referer # 从哪个网站链接过来的 直接访问是没有的
$http_user_agent # 客户端信息 操作系统 版本浏览器信息
$http_x_forwarded_for # 如果前端有代理则记录真实的客户端IP地址

通过Nginx -t检测文件是否正确

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

查看当前和服务器建立的连接数

[root@web01 nginx]# netstat -an|grep ES
tcp        0     52 10.0.0.7:22             10.0.0.1:57777          ESTABLISHED
tcp        0      0 10.0.0.7:80             10.0.0.1:61033          ESTABLISHED

Nginx网站配置

1.新增nginx配置文件

[root@web01 conf.d]# cat /etc/nginx/conf.d/game.conf 
server {
    listen 80;
    server_name game.ahui.com;

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

2.放置游戏源代码文件至nginx配置文件root指定的目录

[root@web01 conf.d]# mkdir /code && cd /code
[root@web01 code]# rz  html5.zip
[root@web01 code]# unzip html5.zip
[root@web01 code]# ls
ceshi  game  html5.zip  img  index.html  readme.txt

3.检查nginx的语法是否存在错误

[root@web01 code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4.重载Nginx [reload|restart]

[root@web01 code]# systemctl reload nginx

5.设置hosts
10.0.0.7 game.ahui.com

6.检测是否设置成功

[c:\~]$ ahui$ ping game.ahui.com
PING game.ahui.com (10.0.0.7): 56 data bytes
64 bytes from 10.0.0.7: icmp_seq=0 ttl=64 time=0.267 ms
64 bytes from 10.0.0.7: icmp_seq=1 ttl=64 time=0.450 ms
64 bytes from 10.0.0.7: icmp_seq=2 ttl=64 time=0.508 ms
64 bytes from 10.0.0.7: icmp_seq=3 ttl=64 time=0.464 ms

7.浏览器检测
打开浏览器访问:http://game.ahui.com
在这里插入图片描述

Nginx配置方式

多域名方式配置

不同的域名对应不同的业务:
game1.ahui.com -----> 玛丽
game2.ahui.com -----> xiaoniao
game3.ahui.com -----> 不能死

[root@web01 conf.d]# cat *.conf
server {
	listen 80;
	server_name game1.ahui.com; 

	location / {
	root /code/mali/;
	index index.html Index.htm;
	}

}
server {
	listen 80;
	server_name game2.ahui.com; 

	location / {
	root /code/38612/;
	index index.html Index.htm;
	}

}
server {
	listen 80;
	server_name game3.ahui.com; 

	location / {
	root /code/xq/;
	index index.html Index.htm;
	}

}

域名对应的代码目录

[root@web01 ~]# ll /code/
total 4
drwxr-xr-x 4 root root  110 Jun 15 12:17 38612
drwxr-xr-x 4 root root  150 Jun 16 09:50 mali
drwxr-xr-x 5 root root 4096 Jun 16 09:52 xq

测试并重启nginx:

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx

上面配置方式通过多主机的方式进行配置实际用了一个域名 只申请一个域名
服务器: 116.63.0.10
ahui.com # 我购买的域名
www.ahui.com —> 玛丽    DNS域名解析后台配置DNS解析: www.ahui.com —> 116.63.0.10
xiaoniao.ahui.com ----> 小鸟                              xiaoniao.ahui.com --> 116.63.0.10
bns.ahui.com ----> 不能死                                bns.ahui.com ----> 116.63.0.10

使用多域名方式进行配置: 域名不能相同 必须申请三个域名
www.mali.com
www.xiaoniao.com
www.bns.com

注意: 可以在一个.conf中写多个server区块

[root@web01 conf.d]# cat test.conf 
server {
	listen 80;
	server_name www.mali.com;

	location / {
	root /code/mali/;
	index index.html index.htm;
	}
}
server {
        listen 80;
        server_name www.xiaon.com;

        location / {
        root /code/xq/;
        index index.html index.htm;
        }
}
server {
        listen 80;
        server_name www.bns.com;

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

单网卡多个IP地址配置方式

配置网卡多IP:

[root@web01 ~]# ip addr add 10.0.0.8/24 dev eth0
[root@web01 ~]# ip addr add 10.0.0.9/24 dev eth0

第一个业务监听在10.0.0.7IP地址

[root@web01 conf.d]# cat xiaoniao.conf 
server {
	listen 10.0.0.7:80;
	server_name _; 

	location / {
	root /code/38612/;
	index index.html Index.htm;
	}

}

第二个业务监听在10.0.0.8 IP地址

[root@web01 conf.d]# cat mali.conf
server {
	listen 10.0.0.8:80;
	server_name _; 

	location / {
	root /code/mali/;
	index index.html Index.htm;
	}

}

第三个业务监听在10.0.0.9 IP地址

[root@web01 conf.d]# cat xq.conf 
server {
	listen 10.0.0.9:80;
	server_name _; 

	location / {
	root /code/xq/;
	index index.html Index.htm;
	}
}

配置完成后需要重启Nginx 新增加的配置文件需要重启Nginx 如果不加新文件只修改可以使用reload

[root@web01 conf.d]# systemcl restart nginx

Nginx多端口配置方式

在内部服务器中 或者后台管理页面都使用不同端口

80对应玛丽业务

	[root@web01 conf.d]# cat mali.conf 
	server {
		listen 80;
		server_name www.ahui.com; 

		location / {
		root /code/mali/;
		index index.html Index.htm;
		}

	}

81端口对应 38业务

[root@web01 conf.d]# cat xiaoniao.conf 
server {
	listen 81;
	server_name www.ahui.com; 

	location / {
	root /code/38612/;
	index index.html Index.htm;
	}

}

82端口对应xq业务

[root@web01 conf.d]# cat 
mali.conf      xiaoniao.conf  xq.conf        
[root@web01 conf.d]# cat xq.conf 
server {
	listen 82;
	server_name www.ahui.com; 

	location / {
	root /code/xq/;
	index index.html Index.htm;
	}

}

检测并重启Nginx

[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx

后台: 管理员登陆 用户不能登陆后台
www.b13king.com:8000 -------------> 商城订单管理
www.b13king.com:9000 -------------> 商城用户管理

Nginx日志管理

Nginx有非常灵活的日志记录模式,每个级别的配置可以有各自独立的访问日志。日志格式通过log_format命令定义格式。

log_format详解
在nginx默认的配置文件中,log_format已经将日志格式定死,但我们可以将其修改。

1.log_format的作用是定义日志格式语法

# 配置语法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default: log_format combined "...";
Context: http

2.nginx默认日志格式语法如下:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

3.Nginx日志格式允许包含的内置变量

$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地址和客户端请求的服务器地址。

4.access_log日志配置语法

Syntax: access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
Default: access_log logs/access.log combined;
Context: http, server, location, if in location, limit_except

5.Nginx Access日志配置实践

server {
    listen 80;
    server_name code.ahui.com;

    #将当前的server网站的访问日志记录至对应的目录,使用main格式
    access_log /var/log/nginx/code.ahui.com.log main;
    location / {
        root /code;
    }

    #当有人请求改favicon.ico时,不记录日志
    location /favicon.ico {
        access_log off;
        return 200;
    }
}

Nginx日志切割

为了防止日志丢失 或者 日志文件过大 将原有的日志文件移动成以时间命名的文件,然后服务重载生成新的日志文件

使用logrotate切割日志
Nginx日志系统默认的logrotate切割

[root@web01 ~]# cat /etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily                   # 每天切割日志
        missingok               # 日志丢失忽略
        rotate 52               # 日志保留52天
        compress                # 日志文件压缩
        delaycompress           # 延迟压缩日志
        notifempty              # 不切割空文件
        create 640 nginx adm    # 日志文件权限
        sharedscripts
        postrotate      # 切割日志执行的命令
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}

日志切割后的效果

[root@web01 ~]# ll /var/log/nginx/
total 4044
-rw-r----- 1 www adm  54438 Oct 12 03:28 access.log-20181012.gz
-rw-r----- 1 www adm  28657 Oct 13 03:48 access.log-20181013.gz
-rw-r----- 1 www adm  10135 Oct 12 03:28 error.log-20181130.gz
-rw-r----- 1 www adm   7452 Oct 13 03:48 error.log-20181201.gz

goaccess 日志监控

第一步: 安装

[root@web01 ~]# yum -y install goaccess

第二步: 配置

[root@web01 ~]# vim /etc/goaccess/goaccess.conf
time-format %H:%M:%S
date-format %d/%b/%Y
\# NCSA Combined Log Format
log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

第三步: 通过命令行测试

[root@web01 ~]# goaccess -f /var/log/nginx/access.log

第四步: 希望把页面存储下来使用浏览器进行访问

vim go.ahui.com
server {
        listen 80;
        server_name go.oldboy.com;

        location / {
        root /code/log;
        index index.html;
        }
}
{root@web01 ~]# mkdir /code/log
[root@web01 ~]# nohup goaccess -f /var/log/nginx/access.log -o /code/log/index.html -p /etc/goaccess/goaccess.conf --real-time-html &

定时任务

有的服务默认不进行切割 使用脚本+定时任务方式进行日志切割:

[root@web01 ~]# crontab -e
mv /var/log/nginx/access.log  /var/log/nginx/access_`date +%F`.log

效果展示

[root@web01 ~]# ll /var/log/nginx/
total 284
-rw-r----- 1 nginx adm 157903 Jun 16 10:50 access.log
-rw-r----- 1 nginx adm 130022 Jun 16 10:50 error.log
[root@web01 ~]# mv /var/log/nginx/access.log  /var/log/nginx/access_`date +%F`.log
[root@web01 ~]# ll /var/log/nginx/
total 284
-rw-r----- 1 nginx adm 157903 Jun 16 10:50 access_2022-06-16.log
 
重新加载服务生成日志文件
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# ll /var/log/nginx/
total 284
-rw-r----- 1 nginx adm  157903 Jun 16 10:50 access_2022-06-16.log
-rw-r--r-- 1 root  root      0 Jun 16 11:27 access.log
-rw-r----- 1 nginx adm  130777 Jun 16 11:27 error.log
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值