简介
nginx解决了apache的c10k问题
IO模型
同步阻塞:一直等待到对方完成
同步非阻塞:一直等待到对方完成后通知
异步阻塞:等待到对方完成
异步非阻塞:交给对方任务后,执行其他任务,等待对方完成后通知
nginx前台运行
-g设置全局指令
[root@centos8 ~]#nginx -g "daemon off;"
树状显示进程
[root@centos8 ~]#ps auxf|grep nginx
root 10287 0.0 0.0 12136 1044 pts/2 S+ 20:10 0:00 \_ grep --color=auto nginx
root 10132 0.0 0.0 42572 3836 ? Ss 20:09 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 10276 0.0 0.0 77316 4912 ? S 20:10 0:00 \_ nginx: worker process
nginx 10277 0.0 0.0 77316 4912 ? S 20:10 0:00 \_ nginx: worker process
nginx平滑升级
查看系统当前使用的nginx信息
[root@centos8 ~]#nginx -v
nginx version: nginx/1.18.0
#-V查看相信的nginx版本信息 比如 nginx编译时都启用了那些参数
[root@centos8 nginx-1.20.1]#nginx -V
nginx version: nginx/1.18.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos8 ~]#systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/nginx.service.d
└─php-fpm.conf
Active: active (running) since Thu 2022-02-24 16:09:48 CST; 38min ago
Docs: http://nginx.org/en/docs/
Process: 9894 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 9963 ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 9964 (nginx)
Tasks: 3 (limit: 50412)
Memory: 2.8M
CGroup: /system.slice/nginx.service
├─9964 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
├─9965 nginx: worker process
└─9966 nginx: worker process
Feb 24 16:09:48 centos8.kktb.org systemd[1]: Starting nginx - high performance web server...
Feb 24 16:09:48 centos8.kktb.org systemd[1]: Started nginx - high performance web server.
#进程树方式查看nginx进程
[root@centos8 ~]#ps -auxf |grep nginx
root 10409 0.0 0.0 12136 1120 pts/4 S+ 16:56 0:00 \_ grep --color=auto nginx
root 9964 0.0 0.0 42440 844 ? Ss 16:36 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 9965 0.0 0.0 77104 5004 ? S 16:36 0:00 \_ nginx: worker process
nginx 9966 0.0 0.0 77104 4972 ? S 16:36 0:00 \_ nginx: worker process
下载&解压&编译新版的nginx
[root@centos8 src]#wget http://nginx.org/download/nginx-1.20.0.tar.gz
[root@centos8 src]#tar xf nginx-1.20.1.tar.gz
#编译新版的nginx 可以使用原来的nginx编译参数,也可以新增参数
[root@centos8 src]#cd nginx-1.20.1/
[root@centos8 nginx-1.20.1]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@centos8 nginx-1.20.1]#make
#编译完成之后会发现多了一个objs目录
[root@centos8 nginx-1.20.1]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
#objs目录下的nginx就是要升级的nginx程序
[root@centos8 nginx-1.20.1]#ll objs/nginx
-rwxr-xr-x 1 root root 7728624 Feb 24 15:07 objs/nginx
备份原来的nginx
[root@centos8 nginx-1.20.1]#mv /apps/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
测试
生成一个大文件用于测试平滑升级时nginx的状态
[root@centos8 ~]#dd if=/dev/zero of=/apps/nginx/html/m1.img bs=1M count=1024
1024+0 records in
1024+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 4.42444 s, 243 MB/s
客户端开始连接nginx下载文件,限制速率为1M/s
[root@centos7 ~]#wget --limit-rate=1M http://10.0.0.5/m1.img
--2022-02-24 16:34:18-- http://10.0.0.5/m1.img
Connecting to 10.0.0.5:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073741824 (1.0G) [application/octet-stream]
Saving to: ‘m1.img.1’
0% [ ] 4,279,936 1024KB/s eta 17m 0s
查看nginx服务状态
[root@centos8 ~]#ss -ntp
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
ESTAB 0 0 10.0.0.5:22 10.0.0.1:52096 users:(("sshd",pid=5732,fd=5),("sshd",pid=5730,fd=5))
ESTAB 0 0 10.0.0.5:22 10.0.0.1:64542 users:(("sshd",pid=10133,fd=5),("sshd",pid=10131,fd=5))
ESTAB 0 1086848 10.0.0.5:80 10.0.0.7:41186 users:(("nginx",pid=9965,fd=14))
平滑更新
复制新版本的nginx到旧版本的安装目录中
[root@centos8 nginx-1.20.1]#cp /usr/local/src/nginx-1.20.1/objs/nginx /apps/nginx/sbin/
检查新版本的nginx对旧配置文件的兼容性
[root@centos8 nginx-1.20.1]#/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@centos8 nginx-1.20.1]#kill -USR2 `cat /apps/nginx/run/nginx.pid`
[root@centos8 nginx-1.20.1]#ps auxf |grep nginx
root 10650 0.0 0.0 12136 1044 pts/3 S+ 17:09 0:00 | \_ grep --color=auto nginx
root 9964 0.0 0.0 42440 2664 ? Ss 16:36 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 9965 0.0 0.0 77104 5004 ? S 16:36 0:00 \_ nginx: worker process
nginx 9966 0.0 0.0 77104 4972 ? S 16:36 0:00 \_ nginx: worker process
root 10644 0.0 0.0 42472 6064 ? S 17:09 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 10645 0.1 0.0 77224 5088 ? S 17:09 0:00 \_ nginx: worker process
nginx 10646 0.1 0.0 77224 5088 ? S 17:09 0:00 \_ nginx: worker process
#先关闭旧nginx的worker进程,而不关闭nginx主进程方便回滚
#等待旧版本的nginx work进程执行完毕就关闭旧nginx的work进程
#可以看到旧版本的work进程后面是 worker process is shutting down
[root@centos8 nginx-1.20.1]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
[root@centos8 nginx-1.20.1]#ps auxf |grep nginx
root 10687 0.0 0.0 12136 1096 pts/3 S+ 17:12 0:00 | \_ grep --color=auto nginx
root 9964 0.0 0.0 42440 2664 ? Ss 16:36 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 9965 0.0 0.0 77104 5004 ? S 16:36 0:00 \_ nginx: worker process is shutting down
root 10644 0.0 0.0 42472 6064 ? S 17:09 0:00 \_ nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 10645 0.0 0.0 77224 5088 ? S 17:09 0:00 \_ nginx: worker process
nginx 10646 0.0 0.0 77224 5088 ? S 17:09 0:00 \_ nginx: worker process
新的测试
#新打开一个下载文件服务
[root@centos7 ~]#wget --limit-rate=1M http://10.0.0.5/m1.img
--2022-02-24 16:48:15-- http://10.0.0.5/m1.img
Connecting to 10.0.0.5:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073741824 (1.0G) [application/octet-stream]
Saving to: ‘m1.img.2’
4% [====> ] 53,007,320 1.00MB/s eta 16m 14s
#查看服务端nginx状况
[root@centos8 nginx-1.20.1]#lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 9964 root 9u IPv4 65812 0t0 TCP *:http (LISTEN)
nginx 9965 nginx 14u IPv4 75956 0t0 TCP centos8.kktb.org:http->10.0.0.7:41186 (ESTABLISHED)
nginx 10644 root 9u IPv4 65812 0t0 TCP *:http (LISTEN)
nginx 10645 nginx 9u IPv4 65812 0t0 TCP *:http (LISTEN)
nginx 10646 nginx 8u IPv4 75013 0t0 TCP centos8.kktb.org:http->10.0.0.7:41188 (ESTABLISHED)
nginx 10646 nginx 9u IPv4 65812 0t0 TCP *:http (LISTEN)
可以看到已经是新版本的nginx在进行服务了。
Ctrl+C
停止客户端的下载服务(模拟用户此次访问完毕)
[root@centos7 ~]#wget --limit-rate=1M http://10.0.0.5/m1.img
--2022-02-24 16:34:18-- http://10.0.0.5/m1.img
Connecting to 10.0.0.5:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073741824 (1.0G) [application/octet-stream]
Saving to: ‘m1.img.1’
87% [==============================================================================================> ] 944,877,184 1.00MB/s eta 2m 3s ^C
#查看服务端nginx状况 旧版本的nginx work进程都已经关闭,只剩下master进程
[root@centos8 nginx-1.20.1]#ps auxf|grep nginx
root 10644 0.0 0.0 42472 6064 pts/3 S+ 18:17 0:00 \_ grep --color=auto nginx
root 10645 0.0 0.3 77224 5088 ? S 18:09 0:00 nginx: master process /apps/nginx/sbin/nginx
nginx 10646 0.0 0.2 77224 5088 ? S 18:09 0:00 \_ nginx: worker process
虚拟主机
同端口不同域名
[root@centos8 ~]#mkdir /data/nginx/html/web1 -pv
mkdir: created directory '/data/nginx'
mkdir: created directory '/data/nginx/html'
mkdir: created directory '/data/nginx/html/web1'
[root@centos8 ~]#mkdir /data/nginx/html/web2 -pv
mkdir: created directory '/data/nginx/html/web2'
[root@centos8 ~]#echo "web1" >> /data/nginx/html/web1/index.html
[root@centos8 ~]#echo "web2" >> /data/nginx/html/web2/index.html
配置文件
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 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name web1.kktb.org;
location / {
root /data/nginx/html/web1;
}
}
server {
listen 80;
server_name web2.kktb.org;
root /data/nginx/html/web2;
location / {
root /data/nginx/html/web2;
}
}
检查语法
[root@centos8 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动服务并加入开机自启
[root@centos8 ~]#systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
测试
#配置hosts文件
[root@centos8 ~]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.8 web1.kktb.org
10.0.0.8 web2.kktb.org
[root@centos8 ~]#curl web1.kktb.org
web1
[root@centos8 ~]#curl web2.kktb.org
web2
不同端口不同域名
配置文件
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 65;
types_hash_max_size 2048;
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;
server {
listen 8809;
server_name web1.kktb.org;
location / {
root /data/nginx/html/web1;
}
}
server {
listen 8909;
server_name web2.kktb.org;
root /data/nginx/html/web2;
location / {
root /data/nginx/html/web2;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
重新加载服务
[root@centos8 ~]#nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@centos8 ~]#nginx -s reload
查看端口
[root@centos8 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:8909 0.0.0.0:*
LISTEN 0 10 127.0.0.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 127.0.0.1:953 0.0.0.0:*
LISTEN 0 128 0.0.0.0:8809 0.0.0.0:*
测试
# 配置本地主机解析地址
[root@centos8 ~]#cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
#10.0.0.8 web1.kktb.org
#10.0.0.8 web2.kktb.org
10.0.0.8 www.kktb.org
# 测试
[root@centos8 ~]#curl 10.0.0.8:8809
web1
[root@centos8 ~]#curl 10.0.0.8:8909
web2
nginx配置文件
location
几种优先级测试
loading…