Nginx概述
Nginx 功能介绍
-
静态的web资源服务器html,图片,js,css,txt等静态资源
-
http/https协议的反向代理 7层
-
tcp/udp协议的请求转发(反向代理) 4层
Nginx作为web服务器与Apache比较
-
模块化设计,目前主流 各类模块拓展性高
-
低内存消耗:10000个keep-alive连接模式下的非活动
-
nginx相较于apache 资源占用更少,支持更高的并发连接 也就拥有更高的效率、
apache是同步多进程 一个连接对应一个进程 nginx 是异步多个连接可以对应一个进程
nginx 拥有更好的静态文件处理能力 好内存少
Apache在处理动态方面有优势
nginx 并发性更好 cpu 占低
nginx 模块
分为 核心模块 标准HTTP模块 可选HTTP模块 邮件服务模块 Stream服务模块 第三方模块
核心模块:是维持nginx服务器正常运作的重要组成部分 是对错误日志的记录 对配置文件进行解析事件驱动机制 进程管理等 核心模块
标准HTTP模块:提供 HTTP 协议解析相关的功能,比如: 端口配置 、 网页编码设置 、 HTTP响应头设置 等
可选HTTP模块:主要用于扩展标准的 HTTP 功能让 Nginx 能处理一些特殊的服务
邮件服务模块:主要用于支持 Nginx 的 邮件服务
Stream服务模块: 实现反向代理功能,包括TCP协议代理
第三方模块: 为nginx提供了更高的拓展性,
核心模块:core module
标准模块:
HTTP 模块: ngx_http_*
HTTP Core modules #默认功能
HTTP Optional modules #需编译时指定
Mail 模块: ngx_mail_*
Stream 模块 ngx_stream_*
第三方模块
编译安装nginx
关闭防火墙和selinux
systemctl stop
firewalld setenforce 0
https://nginx.org/en/download.html
nginx 社区官网
yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
#安装依赖包
useradd -M -s /sbin/nologin nginx
#新建nginx用户便于管理
cd /opt/
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
mkdir /apps/nginx -p
./configure --help 根据帮助中的信息根据自身需求进行模块的开启或关闭
make
make install
chown -R nginx.nginx /apps/nginx
#修改权限
ll /apps/nginx/
配置文件一般都有范例 是文件名.default结尾,
安装好后生成四个文件功能如下:
conf:保存nginx所有的配置文件 ,其中nginx.conf是nginx服务器的最核心最主要的配置文件,
html:目录中保存了nginx服务器的web文件 此为默认 可以自定义为其他 50x的web文件是默认的错误提示页面
logs:用来保存nginx服务器的访问日志错误日志等日志, 也可自定义
sbin:保存nginx二进制启动脚本
启动停止nginx
以绝对路径启动
/apps/nginx/sbin/nginx
创建软连接 创建后直接 nginx 启动
ln -s /apps/nginx/sbin/nginx /usr/sbin/
停止 nginx 服务
killall nginx
或 使用 system 进行启动等操作
vim /usr/lib/systemd/system/nginx.service
#建立文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
如需修改pid号可执行以下操作
mkdir /apps/nginx/run/
#创建目录
vim /apps/nginx/conf/nginx.conf
#修改配置文件
pid /apps/nginx/run/nginx.pid;
#找到 pid的位置修改
systemctl daemon-reload #重新加载配置
systemctl enable --now nginx #开机自启并立即启动 卡住的话可能是log下已有nginx。pid文件 删除即可
chown -R nginx.nginx /apps/nginx
#修改权限
yum安装
centos7 需要安装epel源
yum install -y epel-release #安装epel源
yum install nginx -y #安装nginx
信号 和 信号使用 平滑升级
-v :显示版本号 并退出
-V :显示版本号和配置
-t : 测试配置然后退出
-T : 测试配置并不保存
-q : 不输出error级别下的信息,配合-t参数使用
-s signal : 发送信息是使主进程关闭 退出 重新加载
stop :立即关闭nginx
quit : 优雅的退出nginx 将在运行的任务执行完成后退出
reload : 重新加载
-p prefix : set prefix path (default: /etc/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : 指定配置 不已配置文件中的为准
USR1分割日志
[root@node2 nginx]#cd /var/log/nginx/
#切换到
[root@node2 nginx]#mv access.log access.log.bak
[root@node2 nginx]#touch access.log
#此时日志不会写入到新文件
需要给master 进程发送 USR1信号
[root@node2 nginx]#ps aux |grep nginx
root 117994 0.0 0.0 49024 1084 ? Ss 11:23 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 117995 0.0 0.0 51512 2156 ? S 11:23 0:00 nginx: worker process
nginx 117996 0.0 0.0 51512 1932 ? S 11:23 0:00 nginx: worker process
root 120458 0.0 0.1 151664 5048 pts/1 S+ 12:32 0:00 vim /usr/lib/systemd/system/nginx.serv
[root@node2 nginx]#kill -s USR1 117994
[root@node2 nginx]#nginx -s Reopen 117994
平滑升级nginx
[root@localhost ~]#ps aux |grep nginx
#先查看是否开启nginx
root 26603 0.0 0.0 46204 1160 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 26604 0.0 0.1 48736 2240 ? S 04:58 0:00 nginx: worker process
root 30924 0.0 0.0 118744 1152 pts/1 S+ 10:10 0:00 man /opt/nginx-1.18.0/man/nginx.8
root 31035 0.0 0.0 112824 976 pts/2 S+ 10:16 0:00 grep --color=auto nginx
[root@localhost ~]#vim /apps/nginx/conf/nginx.conf
#开启 两核
#user nobody;
worker_processes 2;
#worker_processes 1 原来是1核
[root@localhost ~]#nginx -s reload
#重新加载配置文件
[root@localhost ~]#ps aux |grep nginx
root 26603 0.0 0.1 46344 2012 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 30924 0.0 0.0 118744 1152 pts/1 S+ 10:10 0:00 man /opt/nginx-1.18.0/man/nginx.8
nginx 31132 0.0 0.1 48856 2092 ? S 10:17 0:00 nginx: worker process
nginx 31133 0.0 0.1 48856 2080 ? S 10:17 0:00 nginx: worker process
##此处多了一个子进程
root 31158 0.0 0.0 112824 972 pts/2 S+ 10:19 0:00 grep --color=auto nginx
[root@localhost ~]#ps auxf |grep nginx
#查看进程树
root 30924 0.0 0.0 118744 1152 pts/1 S+ 10:10 0:00 | \_ man /opt/nginx-1.18.0/man/nginx.8
root 31184 0.0 0.0 112824 976 pts/2 S+ 10:20 0:00 \_ grep --color=auto nginx
root 26603 0.0 0.1 46344 2012 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 31132 0.0 0.1 48856 2092 ? S 10:17 0:00 \_ nginx: worker process
nginx 31133 0.0 0.1 48856 2080 ? S 10:17 0:00 \_ nginx: worker process
[root@localhost ~]#wget https://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src/
#下载安装包到src目录
[root@localhost ~]#cd /usr/local/src/
[root@localhost src]#ls
nginx-1.20.2.tar.gz
[root@localhost src]#tar xf nginx-1.20.2.tar.gz
[root@localhost src]#cd nginx-1.20.2/
[root@localhost nginx-1.20.2]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
########################################################################
这时需要重新编译安装 ./configur 安装参数基本一致 这时可以使用 nginx -V 查看
########################################################################
[root@localhost nginx-1.20.2]#nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
########################################################################
如果 有新模块在后添加即可
########################################################################
[root@localhost nginx-1.20.2]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
#重新编译
[root@localhost nginx-1.20.2]#make
###########注意不要执行 make install
[root@localhost objs]#cd objs
#此文件夹中有新版本的nginx 运行程序
[root@localhost nginx-1.20.2]#objs/nginx -v
#查看版本
nginx version: nginx/1.20.2
[root@localhost nginx-1.20.2]#mv /apps/nginx/sbin/nginx /apps/nginx/sbin/nginx.bak
#将低版本的nginx主程序改名
[root@localhost nginx-1.20.2]#cp objs/nginx /apps/nginx/sbin/
#将新版本 拷入进去
[root@localhost nginx-1.20.2]#ll /apps/nginx/sbin/
总用量 15308
-rwxr-xr-x. 1 root root 7896080 4月 21 10:46 nginx
-rwxr-xr-x. 1 root root 7774624 4月 19 10:43 nginx.bak
[root@localhost nginx-1.20.2]#/apps/nginx/sbin/nginx -t
#检查下语法问题
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.20.2]#kill -USR2 `cat /apps/nginx/run/nginx.pid`
#发送 2 信号 信号在 man手册中可以看到
SIGNALS
The master process of nginx can handle the following signals:
SIGINT, SIGTERM Shut down quickly.
SIGHUP Reload configuration, start the new worker process with a new configuration,
and gracefully shut down old worker processes.
SIGQUIT Shut down gracefully.
SIGUSR1 Reopen log files.
SIGUSR2 Upgrade the nginx executable on the fly.
# 飞行中升级
SIGWINCH Shut down worker processes gracefully.
While there is no need to explicitly control worker processes normally, they support some sig‐
nals too:
SIGTERM Shut down quickly.
SIGQUIT Shut down gracefully.
SIGUSR1 Reopen log files.
[root@localhost nginx-1.20.2]#ps auxf|grep nginx
#生成新的master
root 34765 0.0 0.0 112824 976 pts/2 S+ 10:53 0:00 \_ grep --color=auto nginx
root 26603 0.0 0.1 46344 2012 ? Ss 04:58 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 31132 0.0 0.1 48856 2092 ? S 10:17 0:00 \_ nginx: worker process
nginx 31133 0.0 0.1 48856 2080 ? S 10:17 0:00 \_ nginx: worker process
root 34761 0.0 0.1 46220 3360 ? S 10:53 0:00 \_ nginx: master process /apps/ngin/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 34762 0.0 0.1 48756 1988 ? S 10:53 0:00 \_ nginx: worker process
nginx 34763 0.0 0.1 48756 1988 ? S 10:53 0:00 \_ nginx: worker process
[root@localhost nginx-1.20.2]#lsof -i :80
#查看谁在监听 80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 26603 root 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 31132 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 31133 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 34761 root 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 34762 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
nginx 34763 nginx 6u IPv4 73696 0t0 TCP *:http (LISTEN)
检验
[root@localhost html]#dd if=/dev/zero of=/apps/nginx/html/m.img bs=1G count=10
记录了10+0 的读入
记录了10+0 的写出
10737418240字节(11 GB)已复制,19.3238 秒,556 MB/秒
[root@localhost html]#ls
50x.html index.html m.img
[root@localhost html]#pwd
/apps/nginx/html
#开启新机器下载
[root@localhost data]#wget --limit-rate=1M http://192.168.91.100/m.img
--2022-04-21 11:05:14-- http://192.168.91.100/m.img
正在连接 192.168.91.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10737418240 (10G) [application/octet-stream]
正在保存至: “m.img”
#回到网页服务器
[root@localhost html]#ss -ntap|grep 80
#查看那个进程在管理 下载
LISTEN 0 128 *:80 *:* users:(("nginx",pid=34763,fd=6),("nginx",pid=34762,fd=6),("nginx",pid=34761,fd=6),("nginx",pid=26603,fd=6))
ESTAB 0 993328 192.168.91.100:80 192.168.91.101:52402 users:(("nginx",pid=34763,fd=7))
[root@localhost man]#ls /apps/nginx/run/
#会有 新老两个进程
nginx.pid nginx.pid.oldbin
[root@localhost man]#cat /apps/nginx/run/nginx.pid.oldbin
26603
[root@localhost man]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
#优雅关闭老进程的 worker 进程
#再开启一台服务器测试 是否是新的进程 在下载
[root@localhost ~]# wget --limit-rate=1M http://192.168.91.100/m.img
--2022-04-21 11:14:36-- http://192.168.91.100/m.img
正在连接 192.168.91.100:80... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:10737418240 (10G) [application/octet-stream]
正在保存至: “m.img”
#回到网页服务器可以看到 之前的在 担忧开启了一个 102
[root@localhost html]#ss -ntap|grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=34763,fd=6),("nginx",pid=34762,fd=6),("nginx",pid=34761,fd=6),("nginx",pid=26603,fd=6))
ESTAB 0 993328 192.168.91.100:80 192.168.91.101:52402 users:(("nginx",pid=34763,fd=7))
ESTAB 0 957128 192.168.91.100:80 192.168.91.102:44266 users:(("nginx",pid=34763,fd=12))
测试一段时间无问题 就可以了,如果断掉第一个 下载 ,老的进程就关闭了
[root@localhost html]#pstree -p |grep nginx
#查看进程关系 1.18
|-nginx(26603)
--nginx(work 23333)
--nginx(34761M)-+-nginx(34762)
| 1.20 `-nginx(34763)