一、Nginx
1.Nginx简单介绍
1.什么是Nginx?
Nginx是一个 轻量级/高性能的反向代理Web服务器,他实现非常高效的反向代理、负载均衡,可以处理2-3万并发连接数,官方监测能支持5万并发,现在中国使用nginx网站用户有很多,例如:新浪、网易、 腾讯等。
2.为什么要用Nginx?
1.跨平台、配置简单、反向代理、高并发连接:处理2-3万并发连接数,官方监测能支持5万并发,内存消耗小:开启10个nginx才占150M内存 ,nginx处理静态文件好,耗费内存少
2.而且Nginx内置的健康检测功能:如果有一个服务器宕机,会做一个健康检查,再发送的请求就不会发送到宕机的服务器了。重新将请求提交到其他的节点上。
3.使用Nginx的话还能:
节省宽带:支持GZIP压缩,可以添加浏览器本地缓存
稳定性高:宕机的概率非常小
接收用户请求是异步的
3.Nginx应用场景?
1.http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。
2.反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会应为某台服务器负载高宕机而某台服务器闲置的情况。
3.虚拟主机。可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机。
4.nginx 中也可以配置安全管理、比如可以使用Nginx搭建API接口网关,对每个接口服务进行拦截。
1.Nginx的源码安装
nginx部署
安装软件依赖性
[root@server1 ~]# yum install -y gcc pcre-devel openssl-devel
[root@server1 ~]# tar zxf nginx-1.22.0.tar.gz
[root@server1 ~]# cd nginx-1.22.0/
[root@server1 nginx-1.22.0]# vim auto/cc/gcc
# debug
#CFLAGS="$CFLAGS -g" #禁用debug
编译安装
[root@server1 nginx-1.22.0]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio
[root@server1 nginx-1.22.0]# make
[root@server1 nginx-1.22.0]# make install
[root@server1 nginx-1.22.0]# cd /usr/local/nginx
[root@server1 nginx]# ls
conf html logs sbin
制作软连接
[root@server1 sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@server1 ~]# nginx -t #检测语法
[root@server1 ~]# nginx #启动服务
[root@server1 ~]# nginx -s reload #重载服务
[root@server1 ~]# nginx -s stop #停止服务
配置nginx启动文件
[root@server1 ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@server1 ~]# systemctl daemon-reload
[root@server1 ~]# systemctl enable --now nginx
[root@server1 ~]# curl localhost
2.平滑升级与回滚
# 安装包解压安装
[root@server1 ~]# tar zxf nginx-1.22.1.tar.gz
[root@server1 ~]# cd nginx-1.22.1/
[root@server1 nginx-1.22.1]# vim auto/cc/gcc
# 将debug注释,应用占用空间少
# debug
#CFLAGS="$CFLAGS -g"
[root@server1 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-threads --with-file-aio
[root@server1 nginx-1.22.1]# make
make之后不要执行make install
# 在升级之前备份原程序
[root@server1 nginx-1.22.1]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@server1 nginx-1.22.1]# \cp -f objs/nginx /usr/local/nginx/sbin/
[root@server1 nginx-1.22.1]# cd /usr/local/nginx/sbin/
[root@server1 sbin]# ll
total 1896
-rwxr-xr-x 1 root root 967560 Mar 22 16:03 nginx
-rwxr-xr-x 1 root root 967560 Mar 22 16:03 nginx.bak
[root@server1 sbin]# ps ax |grep nginx
12812 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
12813 ? S 0:00 nginx: worker process
15528 pts/0 S+ 0:00 grep --color=auto nginx
# 旧版本的主进程
[root@server1 sbin]# kill -USR2 12812
[root@server1 sbin]# ps ax |grep nginx
12812 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
12813 ? S 0:00 nginx: worker process
15530 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15531 ? S 0:00 nginx: worker process
15533 pts/0 S+ 0:00 grep --color=auto nginx
# 此时访问还是旧版本
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Wed, 22 Mar 2023 08:04:30 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 22 Mar 2023 07:59:35 GMT
Connection: keep-alive
ETag: "641ab567-267"
Accept-Ranges: bytes
# 关闭旧版本
[root@server1 sbin]# kill -WINCH 12812
[root@server1 sbin]# ps ax |grep nginx
12812 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15530 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15531 ? S 0:00 nginx: worker process
15539 pts/0 S+ 0:00 grep --color=auto nginx
# 成功升级新版本
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Wed, 22 Mar 2023 08:05:04 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 22 Mar 2023 07:59:35 GMT
Connection: keep-alive
ETag: "641ab567-267"
Accept-Ranges: bytes
# 版本回退
[root@server1 sbin]# \cp -f nginx.bak nginx
[root@server1 sbin]# kill -HUP 12812
[root@server1 sbin]# ps ax|grep nginx
12812 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15530 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15531 ? S 0:00 nginx: worker process
15545 ? S 0:00 nginx: worker process
15547 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Wed, 22 Mar 2023 08:05:49 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 22 Mar 2023 07:59:35 GMT
Connection: keep-alive
ETag: "641ab567-267"
Accept-Ranges: bytes
[root@server1 sbin]# kill -WINCH 15530
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.0
Date: Wed, 22 Mar 2023 08:06:11 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 22 Mar 2023 07:59:35 GMT
Connection: keep-alive
ETag: "641ab567-267"
Accept-Ranges: bytes
# 利用版本回退,升级回新版本
[root@server1 sbin]# ps ax|grep nginx
12812 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15530 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15545 ? S 0:00 nginx: worker process
15553 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# kill -HUP 15530
[root@server1 sbin]# kill -WINCH 12812
[root@server1 sbin]# ps ax|grep nginx
12812 ? Ss 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15530 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15554 ? S 0:00 nginx: worker process
15556 pts/0 S+ 0:00 grep --color=auto nginx
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Wed, 22 Mar 2023 08:09:59 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 22 Mar 2023 07:59:35 GMT
Connection: keep-alive
ETag: "641ab567-267"
Accept-Ranges: bytes
# 删除旧版本进程
[root@server1 sbin]# kill 12812
[root@server1 sbin]# curl -I localhost
HTTP/1.1 200 OK
Server: nginx/1.22.1
Date: Wed, 22 Mar 2023 08:10:12 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 22 Mar 2023 07:59:35 GMT
Connection: keep-alive
ETag: "641ab567-267"
Accept-Ranges: bytes
[root@server1 sbin]# ps ax|grep nginx
15530 ? S 0:00 nginx: master process /usr/local/nginx/sbin/nginx
15554 ? S 0:00 nginx: worker process
15562 pts/0 S+ 0:00 grep --color=auto nginx