nginx学习
nginx yum安装
yum源安装地址
http://nginx.org/en/linux_packages.html#RHEL
yum源配置地址
[root@centos8 ~]# cat >/etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
查看nginx版本信息
[root@centos8 yum.repos.d]# yum info nginx
Last metadata expiration check: 0:00:20 ago on Fri 23 Dec 2022 04:10:31 PM CST.
Installed Packages
Name : nginx
Epoch : 1
Version : 1.22.1
Release : 1.el8.ngx
Architecture : x86_64
Size : 2.8 M
Source : nginx-1.22.1-1.el8.ngx.src.rpm
Repository : @System
From repo : nginx-stable
Summary : High performance web server
URL : https://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
[root@centos8 yum.repos.d]#
安装nginx
[root@centos8 yum.repos.d]# yum install nginx -y
nginx stable repo 42 kB/s | 47 kB 00:01
Dependencies resolved.
====================================================================================================================
Package Architecture Version Repository Size
====================================================================================================================
Installing:
nginx x86_64 1:1.22.1-1.el8.ngx nginx-stable 828 k
查看nginx配置文件
[root@centos8 yum.repos.d]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
启动nginx
[root@centos8 yum.repos.d]# systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@centos8 yum.repos.d]# ss -ntlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 32 192.168.122.1:53 0.0.0.0:* users:(("dnsmasq",pid=1836,fd=6))
LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=1147,fd=4))
LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=1149,fd=9))
LISTEN 0 128 127.0.0.1:6010 0.0.0.0:* users:(("sshd",pid=3282,fd=15))
LISTEN 0 128 127.0.0.1:6011 0.0.0.0:* users:(("sshd",pid=3920,fd=15))
LISTEN 0 5 127.0.0.1:44321 0.0.0.0:* users:(("pmcd",pid=1720,fd=0))
LISTEN 0 128 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=989,fd=4),("systemd",pid=1,fd=44))
LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=45416,fd=9),("nginx",pid=45415,fd=9))
LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1147,fd=6))
LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=1149,fd=8))
LISTEN 0 128 [::1]:6010 [::]:* users:(("sshd",pid=3282,fd=14))
LISTEN 0 128 [::1]:6011 [::]:* users:(("sshd",pid=3920,fd=14))
LISTEN 0 5 [::1]:44321 [::]:* users:(("pmcd",pid=1720,fd=3))
LISTEN 0 128 [::]:111 [::]:* users:(("rpcbind",pid=989,fd=6),("systemd",pid=1,fd=47))
[root@centos8 yum.repos.d]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-12-23 16:13:14 CST; 23s ago
Docs: http://nginx.org/en/docs/
Process: 45414 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 45415 (nginx)
Tasks: 2 (limit: 11074)
Memory: 2.3M
CGroup: /system.slice/nginx.service
├─45415 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─45416 nginx: worker process
Dec 23 16:13:14 centos8 systemd[1]: Starting nginx - high performance web server...
Dec 23 16:13:14 centos8 systemd[1]: nginx.service: Can't open PID file /var/run/nginx.pid (yet?) after start: No su>
Dec 23 16:13:14 centos8 systemd[1]:
直接网页访问IP地址,默认端口80
查看nginx版本
[root@centos8 yum.repos.d]# nginx -v
nginx version: nginx/1.22.1
[root@centos8 yum.repos.d]# nginx -V
nginx version: nginx/1.22.1
......
nginx使用
[root@centos8 yum.repos.d]# nginx -h
nginx version: nginx/1.22.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显示版本和编译参数
-t : test configuration and exit #测试配置文件是否异
常
-T : test configuration, dump it and exit #测试并打印
-q : suppress non-error messages during configuration testing #静默模
式
-s signal : send signal to a master process: stop, quit, reopen, reload #发
送信号,reload信号 会生成新的worker,但master不会重新生成(nginx -s stop)
-p prefix : set prefix path (default: /etc/nginx/) #指定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 : set global directives out of configuration file #设置全局指令,注意
和配置文件不要同时配置,否则冲突(nginx -g "user test")
nginx启动文件
[root@centos8 ~]# cat /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=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"
[Install]
WantedBy=multi-user.target
nginx 编译安装
[root@localhost ~]# yum -y install gcc pcre-devel openssl-devel zlib-devel
[root@localhost ~]# useradd -s /sbin/nologin nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@localhost ~]# tar xvf nginx-1.18.0.tar.gz
[root@localhost ~]# cd nginx-1.18.0/
[root@localhost nginx-1.18.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@localhost nginx-1.18.0]# ./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.18.0]# make -j 2
[root@localhost nginx-1.18.0]# make install
[root@localhost nginx-1.18.0]# tree /apps/nginx/
/apps/nginx/
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx
4 directories, 18 files
[root@localhost nginx-1.18.0]# ln -s /apps/nginx/sbin/nginx /usr/sbin/
[root@localhost nginx-1.18.0]# nginx -v
nginx version: nginx/1.18.0
[root@localhost nginx-1.18.0]# nginx
[root@localhost nginx-1.18.0]# ll /apps/nginx/
total 4
drwx------. 2 nginx root 6 Dec 23 17:11 client_body_temp
drwxr-xr-x. 2 root root 4096 Dec 23 17:09 conf
drwx------. 2 nginx root 6 Dec 23 17:11 fastcgi_temp
drwxr-xr-x. 2 root root 40 Dec 23 17:09 html
drwxr-xr-x. 2 root root 58 Dec 23 17:11 logs
drwx------. 2 nginx root 6 Dec 23 17:11 proxy_temp
drwxr-xr-x. 2 root root 19 Dec 23 17:09 sbin
drwx------. 2 nginx root 6 Dec 23 17:11 scgi_temp
drwx------. 2 nginx root 6 Dec 23 17:11 uwsgi_temp
[root@localhost nginx-1.18.0]# chown -R nginx.nginx /apps/nginx/
[root@localhost nginx-1.18.0]# ll /apps/nginx/
total 4
drwx------. 2 nginx nginx 6 Dec 23 17:11 client_body_temp
drwxr-xr-x. 2 nginx nginx 4096 Dec 23 17:09 conf
drwx------. 2 nginx nginx 6 Dec 23 17:11 fastcgi_temp
drwxr-xr-x. 2 nginx nginx 40 Dec 23 17:09 html
drwxr-xr-x. 2 nginx nginx 58 Dec 23 17:11 logs
drwx------. 2 nginx nginx 6 Dec 23 17:11 proxy_temp
drwxr-xr-x. 2 nginx nginx 19 Dec 23 17:09 sbin
drwx------. 2 nginx nginx 6 Dec 23 17:11 scgi_temp
drwx------. 2 nginx nginx 6 Dec 23 17:11 uwsgi_temp
创建nginx启动文件
[root@localhost ~]# find /apps -name nginx.pid
/apps/nginx/logs/nginx.pid
[root@localhost ~]# vim /apps/nginx/
client_body_temp/ fastcgi_temp/ logs/ sbin/ uwsgi_temp/
conf/ html/ proxy_temp/ scgi_temp/
[root@localhost ~]# vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
[root@localhost ~]# mkdir /apps/nginx/run
[root@localhost ~]# chown -R nginx.nginx /apps/nginx/run/
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]#
[root@localhost ~]# systemctl enable --now nginx
[root@localhost ~]#
[root@localhost ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2022-12-23 17:39:32 CST; 2min 11s ago
nginx平滑升级
1.将旧的nginx文件换成新的nginx文件(注意备份)
2.向master进行发送USR2信息
3.master进程修改PID文件名,加后缀.oldbin
4.master进程用新到nginx文件启动新的master进程,系统中将有新旧两个nginx主进程共同提供web服务
5.向旧的nginx服务进程发送WINCH信号,使旧的nginx worker进程平滑停止,并删除nginx.pid.oldbin文件
6.向旧master进程发送QUIT信息,关闭老master
7.如果发现升级有问题,可以回滚:向老master发送HUP,向新的master发送QUIT
下载新版nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.2.tar.gz -P /usr/local/src
解压
使用原来选项1.18版本进行编译-V 查看
[root@localhost nginx-1.20.2]# nginx -V
nginx version: nginx/1.18.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-17) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
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
编译(只make不需要make install)
[root@localhost nginx-1.20.2]# make
备份旧版本nginx
[root@localhost nginx-1.20.2]# mv /apps/nginx/sbin/nginx{,.bak}
[root@localhost nginx-1.20.2]# ll /apps/nginx/sbin/
total 7416
-rwxr-xr-x. 1 nginx nginx 7591176 Dec 23 17:09 nginx.bak
[root@localhost nginx-1.20.2]#
拷贝新版本到安装目录
[root@localhost nginx-1.20.2]# cp objs/nginx /apps/nginx/sbin/
[root@localhost nginx-1.20.2]#
[root@localhost nginx-1.20.2]# ll /apps/nginx/sbin/
total 14964
-rwxr-xr-x 1 root root 7726400 Jan 3 11:11 nginx
-rwxr-xr-x. 1 nginx nginx 7591176 Dec 23 17:09 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]# ps -aux |grep nginx
root 1393 0.0 0.2 42572 3844 ? Ss 09:12 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 8274 0.0 0.2 77304 4908 ? S 10:58 0:00 nginx: worker process
nginx 8275 0.0 0.2 77304 4908 ? S 10:58 0:00 nginx: worker process
root 11687 0.0 0.0 12112 984 pts/0 S+ 11:13 0:00 grep --color=auto nginx
[root@localhost nginx-1.20.2]# cat /apps/nginx/run/nginx.pid
1393
[root@localhost nginx-1.20.2]# kill -USR2 `cat /apps/nginx/run/nginx.pid`
[root@localhost nginx-1.20.2]# ps -aux |grep nginx
root 1393 0.0 0.2 42572 3844 ? Ss 09:12 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 8274 0.0 0.2 77304 4908 ? S 10:58 0:00 nginx: worker process
nginx 8275 0.0 0.2 77304 4908 ? S 10:58 0:00 nginx: worker process
root 11717 0.0 0.3 42464 5992 ? S 11:14 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 11718 0.0 0.2 77208 5072 ? S 11:14 0:00 nginx: worker process
nginx 11719 0.0 0.2 77208 5072 ? S 11:14 0:00 nginx: worker process
root 11723 0.0 0.0 12112 1088 pts/0 R+ 11:14 0:00 grep --color=auto nginx
关闭旧的nginx worker进程不关闭nginx主进程方便回滚
[root@localhost nginx-1.20.2]# cat /apps/nginx/run/nginx.pid.oldbin
1393
[root@localhost nginx-1.20.2]# cat /apps/nginx/run/nginx.pid
11717
[root@localhost nginx-1.20.2]# kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
[root@localhost nginx-1.20.2]# ps -aux |grep nginx
root 1393 0.0 0.2 42572 3844 ? Ss 09:12 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 11717 0.0 0.3 42464 5992 ? S 11:14 0:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 11718 0.0 0.2 77208 5072 ? S 11:14 0:00 nginx: worker process
nginx 11719 0.0 0.2 77208 5072 ? S 11:14 0:00 nginx: worker process
root 11867 0.0 0.0 12112 1088 pts/0 R+ 11:21 0:00 grep --color=auto nginx
长时间测试后新版本没有问题,最后退出老的master
kill -QUIT `cat /apps/nginx/run/nginx.pid.oldbin`
查看是否是新版
[root@localhost logs]# curl -I 127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Tue, 03 Jan 2023 03:37:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 23 Dec 2022 09:09:48 GMT
Connection: keep-alive
ETag: "63a5705c-264"
Accept-Ranges: bytes
[root@localhost logs]# nginx -v
nginx version: nginx/1.20.2
回滚
发现有问题可以重新拉起旧版的worker
kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin`
[root@localhost logs]# kill -HUP `cat /apps/nginx/run/nginx.pid.oldbin`
[root@localhost logs]# ps -ef |grep nginx
root 1393 1 0 09:12 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
root 11717 1393 0 11:14 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 11718 11717 0 11:14 ? 00:00:00 nginx: worker process
nginx 11719 11717 0 11:14 ? 00:00:00 nginx: worker process
nginx 12779 1393 0 11:31 ? 00:00:00 nginx: worker process
nginx 12780 1393 0 11:31 ? 00:00:00 nginx: worker process
root 12784 2502 0 11:31 pts/0 00:00:00 grep --color=auto nginx
最后关闭新版的master
[root@localhost logs]# kill -QUIT `cat /apps/nginx/run/nginx.pid`
[root@localhost logs]# ps -ef |grep nginx
root 1393 1 0 09:12 ? 00:00:00 nginx: master process /apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
nginx 12779 1393 0 11:31 ? 00:00:00 nginx: worker process
nginx 12780 1393 0 11:31 ? 00:00:00 nginx: worker process
关注小张的知识杂货铺,让我们一起学习一起进步