Nginx练习

Nginx

[TOC]

一、Nginx的编译安装

1.1、官方源码包下载地址:

1
https://nginx.org/en/download.html

image-20221127110618803

1.2、编译安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
[root@Rocky ~]#yum -y install gcc pcre-devel openssl-devel zlib-devel     #安装相关包
[root@Rocky ~]#useradd -s /sbin/nologin nginx #创建Nginx账号,因不需要登录,只需要给nologin的shell类型
[root@Rocky ~]#wget https://nginx.org/download/nginx-1.22.1.tar.gz
[root@Rocky ~]#mv nginx-1.22.1.tar.gz /usr/local/src
#/usr/local :用户级的软件目录,用来存放用户安装编译的软件,用户自己编译安装的软件也默认存放在这里
#/usr/local/src :这个目录是存放用户编译软件所用的源码的
[root@Rocky ~]#cd /usr/local/src
[root@Rocky src]#tar xf nginx-1.22.1.tar.gz
[root@Rocky src]#ls
nginx-1.22.1 nginx-1.22.1.tar.gz
[root@Rocky src]#cd nginx-1.22.1/
[root@Rocky nginx-1.22.1]#ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
#一般安装方法可以从README或者INSTALL里面可以找到
[root@Rocky nginx-1.22.1]#./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@Rocky nginx-1.22.1]#make -j 2 && make install
[root@Rocky ~]#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
[root@Rocky ~]#/apps/nginx/sbin/nginx #启动Nginx,后台启动
[root@Rocky ~]#ss -ntl #此时可以看到80端口已监听
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
#此时关闭只有使用kill命令
[root@Rocky ~]#ps auxf
root 29907 0.0 0.0 42572 848 ? Ss 12:05 0:00 nginx: master process /apps/ngin
nginx 29908 0.0 0.2 74680 4948 ? S 12:05 0:00 \_ nginx: worker process
[root@Rocky ~]#kill 29907

#只输入Nginx就可以启动有两种方法
#第一种方法
[root@Rocky ~]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
[root@Rocky ~]#nginx
[root@Rocky ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@Rocky ~]#nginx -s stop
[root@Rocky ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
#第二种方法
[root@Rocky ~]#echo "PATH=/apps/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
#将其路径写入环境变量中
[root@Rocky ~]#source /etc/profile.d/nginx.sh

image-20221127114404902

1.3、设置启动方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#使用nginx此种方式开启,关闭时只能使用kill
#更好的方式是设置service文件
[root@Rocky ~]#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/run/nginx.pid #指定pid文件的目录,默认在logs目录下,可选配置
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID #$MAINPID指的是主进程的pid
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target
:wq
#需要自己创建存放pid的文件目录
[root@Rocky ~]#mkdir /apps/nginx/run
#因为配置文件不知道在哪读取pid,所以需要在配置文件了进行设置
[root@Rocky ~]#vim /apps/nginx/conf/nginx.conf
pid   /apps/nginx/run/nginx.pid; #取消此行注释,并将路径改为此路径
:wq
#注意修改完配置文件后,需先查看是否有语法错误:nginx -t
[root@Rocky ~]#systemctl enable --now nginx
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
[root@Rocky ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*

二、Nginx的平滑升级

2.1、平滑升级的流程

image-20221127155647389

①将旧Nginx二进制文件换成新Nginx程序文件(注意先备份)

②向master进程发送USR2信号启动新nginx进程

③master进程修改pid文件名加上后缀.oldbin,成为nginx.pid.oldbin

④master进程用新Nginx文件启动新master进程及worker子进程成为旧master的子进程,系统中将有新旧两个Nginx主进程和对应的worker子进程并存

当前新的请求仍然由旧Nginx的worker进程进行处理,将新生成的master进程的PID存放至新生成的pid文件nginx.pid

⑤向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止

⑥向旧master进程发送QUIT信号,关闭旧master,并删除Nginx.pid.oldbin文件

⑦如果发现升级有问题,可以回滚∶向旧master发送HUP,向新master发送QUIT

2.2、将旧Nginx二进制文件换成新Nginx程序文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#升级前首先需要将原有的复制保存
[root@Rocky ~]#cp /apps/nginx/sbin/nginx /opt/ #将旧版本复制到opt目录下

[root@Rocky ~]#wget https://nginx.org/download/nginx-1.23.2.tar.gz #下载新版本
[root@Rocky ~]#tar xf nginx-1.23.2.tar.gz #解压缩
[root@Rocky ~]#ls
anaconda-ks.cfg nginx-1.23.2 nginx-1.23.2.tar.gz
[root@Rocky ~]#cd nginx-1.23.2/
[root@Rocky nginx-1.23.2]#nginx -V #查看已安装的Nginx的配置
nginx version: nginx/1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (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
#configure arguments后面是以前编译时的参数。现在编译使用一样的参数

#开始编译新版本
[root@Rocky nginx-1.23.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@Rocky nginx-1.23.2]#make -j 2
[root@Rocky nginx-1.23.2]#ls #编译完成后,多了objs文件,里面有新版的nginx
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@Rocky nginx-1.23.2]#objs/nginx -V
nginx version: nginx/1.23.2
[root@Rocky nginx-1.23.2]#ll objs/nginx /apps/nginx/sbin/nginx
-rwxr-xr-x 1 root root 7587600 Nov 27 16:56 /apps/nginx/sbin/nginx
-rwxr-xr-x 1 root root 7630304 Nov 27 17:11 objs/nginx

#为了便于观察用其他机器下载Nginx的文件
[root@Rocky nginx-1.23.2]#dd if=/dev/zero of=/apps/nginx/html/fi.img bs=1M count=100
#先在Nginx里创建一个100M文件
[root@centos ~]#wget --limit-rate=1024 http://10.0.0.185/fi.img
[root@centos ~]#curl -I 10.0.0.185
HTTP/1.1 200 OK
Server: nginx/1.22.1

#将旧版Nginx替换为新版Nginx
[root@Rocky nginx-1.23.2]#cp -f objs/nginx /apps/nginx/sbin/
cp: overwrite '/apps/nginx/sbin/nginx'? y
[root@Rocky nginx-1.23.2]#ll /apps/nginx/sbin/nginx
-rwxr-xr-x 1 nginx nginx 7630280 Nov 27 16:21 /apps/nginx/sbin/nginx
[root@centos ~]#curl -I 10.0.0.185 #替换之后仍是老版本
HTTP/1.1 200 OK
Server: nginx/1.22.1

#注意此时只是把磁盘文件里的Nginx替换了,内存里的仍旧是旧版Nginx
#下一步首先需要检测新版Nginx和配置文件是否兼容
[root@Rocky nginx-1.23.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
#显示successful,表示语法兼容

2.3、向master进程发送USR2信号启动新nginx进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#发送信号前,只有1个work子进程
[root@Rocky ~]#ps auxf | grep nginx
root 33042 0.0 0.0 42584 852 ? Ss 16:31 0:00 nginx: master process nginx
nginx 33043 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process

[root@Rocky nginx-1.23.2]#kill -USR2 `cat /apps/nginx/run/nginx.pid`
#其中USR2可以写成12,cat /apps/nginx/run/nginx.pid可以直接写成进程编号
[root@Rocky nginx-1.23.2]#kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1
[root@Rocky ~]#ps auxf | grep nginx
root 33042 0.0 0.0 42584 852 ? Ss 16:31 0:00 nginx: master process nginx
nginx 33043 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process
root 33023 0.0 0.0 42584 852 ? Ss 16:31 0:00 \_nginx: master process nginx
nginx 33024 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process

#此时处于新旧并存,旧的还在工作

2.4、向旧的Nginx服务进程发送WINCH信号,使旧的Nginx worker进程平滑停止

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#先关闭旧nginx的worker进程,而不关闭nginx主进程方便回滚
#向原Nginx主进程发送WINCH信号,它会逐步关闭旗下的工作进程(主进程不退出),这时所有请求都会由新版Nginx处理

[root@Rocky nginx-1.23.2]#kill -WINCH `cat /apps/nginx/run/nginx.pid.oldbin`
[root@Rocky ~]#ps auxf | grep nginx
root 33042 0.0 0.0 42584 852 ? Ss 16:31 0:00 nginx: master process nginx
nginx 33043 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process is shutting down
root 33023 0.0 0.0 42584 852 ? Ss 16:31 0:00 \_nginx: master process nginx
nginx 33024 0.0 0.2 74692 4932 ? S 16:31 0:00 \_ nginx: worker process

[root@centos ~]#curl -I 10.0.0.185
HTTP/1.1 200 OK
Server: nginx/1.23.2
#此时旧版的已经不工作了,新版开始工作了。此时仍是新旧共存

2.5、此时新旧共存,测试一段时间新版本是否正常运行

2.5.1、正常运行
1
2
3
4
[root@Rocky nginx-1.23.2]#kill -QUIT `cat /apps/nginx/logs/nginx.pid.oldbin`
#QUIT也可以写成3
[root@Rocky nginx-1.23.2]#nginx -V
nginx version: nginx/1.23.2
2.5.2、不能正常运行
1
2
3
4
5
6
7
8
9
10
11
#回滚
#如果升级的版本发现问题需要回滚,可以发送HUP信号,重新拉起旧版本的worker

[root@Rocky nginx-1.23.2]#kill -HUP `cat /apps/nginx/logs/nginx.pid.oldbin`
#此时又新生成1个work进程
[root@centos ~]#curl -I 10.0.0.185
HTTP/1.1 200 OK
Server: nginx/1.22.1
#最后关闭新版的master
[root@Rocky nginx-1.23.2]#kill -QUIT `cat /apps/nginx/logs/nginx.pid`
[root@Rocky nginx-1.23.2]#cp -f /opt/nginx /apps/nginx/sbin/ #把旧版本在替换回来

三、多虚拟主机

基于不同的IP、不同的端口以及不用得域名实现不同的虚拟主机,依赖于核心模块

ngx_http_core_module实现。

3.1、新建一个PC web站点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@Rocky ~]#mkdir -p /data/nginx/html/pc  #创建页面存放路径
[root@Rocky ~]#vim /data/nginx/html/pc/index.html
<h1> www.magedu.org </h1>
:wq

#下一步设置配置文件,文件目录/apps/nginx/conf/nginx.conf。但是为了后期便于管理,可以自己再创建子目录/apps/nginx/conf.d/,在这个目录下单独创建pc站点
[root@Rocky ~]#mkdir /apps/nginx/conf.d
[root@Rocky ~]#vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf.d/*.conf; #将此行添加在倒数第二行,即是放在http里面,注意不要放在最前面,会导致前面的命令无法生效
[root@Rocky ~]#cd /apps/nginx/conf.d
[root@Rocky conf.d]#vim pc.conf
server {
listen 80;
server_name www.magedu.org;
location / {
root /data/nginx/html/pc;
}
}
:wq

#注意,也可以写成这种形式
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
}

[root@Rocky conf.d]#nginx -s reload

#需要搭建DNS,以前有搭建的DNS服务器(10.0.0.128)
[root@DNS ~]#vim /var/named/magedu.org.zone
$TTL 1D
@ IN SOA master admin.magedu.org ( 6 1D 10M 1D 6H )
NS master
NS slave1
master A 10.0.0.128
slave1 A 10.0.0.184
www A 10.0.0.184
:wq
[root@DNS ~]#rndc reload #重新加载
server reload successful

#用客户端访问,需先将DNS指向10.0.0.128
[root@client ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=10.0.0.128
:wq
[root@client ~]#nmcli connection reload
[root@client ~]#nmcli connection up eth0
[root@client ~]#curl www.magedu.org
<h1> www.magedu.org </h1>

3.2、新建一个Mobile web站点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@Rocky ~]#mkdir -p /data/nginx/html/mobile  #创建页面存放路径
[root@Rocky ~]#vim /data/nginx/html/mobile/index.html
<h1> m.magedu.org </h1>
:wq
[root@Rocky ~]#mkdir /apps/nginx/conf.d
[root@Rocky ~]#vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf.d/*.conf; #将此行添加在倒数第二行,即是放在http里面,注意不要放在最前面,会导致前面的命令无法生效
[root@Rocky ~]#cd /apps/nginx/conf.d
[root@Rocky conf.d]#vim mobile.conf
server {
listen 80;
server_name m.magedu.org;
location / {
root /data/nginx/html/pc;
}
}
:wq
[root@Rocky conf.d]#nginx -s reload

#需要搭建DNS,以前有搭建的DNS服务器(10.0.0.128)
[root@DNS ~]#vim /var/named/magedu.org.zone
$TTL 1D
@ IN SOA master admin.magedu.org ( 6 1D 10M 1D 6H )
NS master
NS slave1
master A 10.0.0.128
slave1 A 10.0.0.184
m A 10.0.0.184
:wq
[root@DNS ~]#rndc reload #重新加载
server reload successful

#用客户端访问,需先将DNS指向10.0.0.128
[root@client ~]#vim /etc/sysconfig/network-scripts/ifcfg-eth0
DNS1=10.0.0.128
:wq
[root@client ~]#nmcli connection reload
[root@client ~]#nmcli connection up eth0
[root@client ~]#curl m.magedu.org
<h1> m.magedu.org </h1>

3.3、还可以在pc站点下再新建一个子目录

3.3.1、在/data/nginx/html/pc新建about
1
2
3
4
5
[root@Rocky ~]#cd /data/nginx/html/pc
[root@Rocky pc]#mkdir about
[root@Rocky pc]#vim about/index.html
<h1> about www.magedu.org </h1>
:wq

image-20221128112020141

3.3.2、在新的目录下创建about
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@Rocky ~]#mkdir -p /opt/pc/about
[root@Rocky ~]#cd /opt/pc/about
[root@Rocky about]#vim index.html
<h1> /opt/pc/about/index.html </h1>
:wq
[root@Rocky ~]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
location /about {
alias /opt/pc/about;
}
}
:wq

image-20221128113152131

四、区分不同网站的访问日志,及设置日志的类型

4.1、自定义日志类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
[root@Rocky ~]#cd /apps/nginx/logs
[root@Rocky logs]#ls
access.log error.log nginx.pid
#此时所有网站的访问日志都在access.log里面

#访问日志的类型设置位于http里面
#log_format是日志类型,放于http里面;access_log是日志的路径,放于http(适用于所有网站)或者server(适用于这一个网站)里面
#日志格式名称随意设置
[root@Rocky logs]#vim /apps/nginx/conf/nginx.conf
http {
.....
log_format testlog '$remote_addr [$time_local] "$request" "$http_referer" $status ';
#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 logs/access.log main;
.....
}
:wq

[root@Rocky logs]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/access-www.magedu.org.log testlog; #添加此行
location /about {
alias /opt/pc/about;
}
}
:wq
[root@Rocky logs]#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@Rocky logs]#nginx -s reload
[root@Rocky logs]#ls
access.log access-www.magedu.org.log error.log nginx.pid

4.2、把日志格式设置成json格式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[root@Rocky logs]#vim /apps/nginx/conf/nginx.conf
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';
:wq
[root@Rocky logs]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json; #添加此行
location /about {
alias /opt/pc/about;
}
}
:wq
[root@Rocky logs]#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@Rocky logs]#nginx -s reload
[root@Rocky logs]#ls
access-json-www.magedu.org.log access.log access-www.magedu.org.log error.log nginx.pid

#现在可以观察
[root@client ~]#curl www.magedu.org
<h1> www.magedu.org </h1>
[root@Rocky logs]#tail -f /apps/nginx/logs/access-json-www.magedu.org.log
{"@timestamp":"2022-11-28T13:28:46+08:00","host":"10.0.0.184","clientip":"10.0.0.185","size":26,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.magedu.org","uri":"/index.html","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.61.1","status":"200"}

#此种形式不方便看,可以使用工具jq
[root@Rocky logs]#yum -y install jq
[root@Rocky logs]#cat /apps/nginx/logs/access-json-www.magedu.org.log |jq
{
"@timestamp": "2022-11-28T13:28:46+08:00",
"host": "10.0.0.184",
"clientip": "10.0.0.185",
"size": 26,
"responsetime": 0.000,
"upstreamtime": "-",
"upstreamhost": "-",
"http_host": "www.magedu.org",
"uri": "/index.html",
"xff": "-",
"referer": "-",
"tcp_xff": "-",
"http_user_agent": "curl/7.61.1",
"status": "200"
}

五、开启流量监控

5.1、开启Nginx的状态页

基于nginx 模块 ngx_http_stub_status_module 实现,在编译安装nginx的时候需要添加编译参数 –with-http_stub_status_module,否则配置完成之后监测会是提示语法错误

注意: 状态页显示的是整个服务器的状态,而非虚拟主机的状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#先确认是否安装 ngx_http_stub_status_module模块,如果没有需要添加
[root@Rocky ~]#nginx -V
nginx version: nginx/1.20.2
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (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@Rocky ~]#vim /apps/nginx/conf.d/pc.conf
location /nginx_status {
stub_status;
}
:wq
[root@Rocky ~]#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@Rocky ~]#nginx -s reload
[root@client ~]#curl http://www.magedu.org/nginx_status
Active connections: 1
server accepts handled requests
33 33 29
Reading: 0 Writing: 1 Waiting: 0

#Active connections: #当前处于活动状态的客户端连接数,包括连接等待空闲连接数=reading+writing+waiting
accepts:#统计总值,Nginx自启动后已经接受的客户端请求连接的总数。
handled:#统计总值,Nginx自启动后已经处理完成的客户端请求连接总数,通常等于accepts,除非有因
worker_connections限制等被拒绝的连接
requests:#统计总值,Nginx自启动后客户端发来的总的请求数。
Reading:#当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足
Writing:#当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大
Waiting:#当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于
active – (reading+writing)

[root@client ~]#curl http://www.magedu.org/nginx_status 2>/dev/null |awk '/Reading/{print $2,$4,$6}'
0 1 0

image-20221128151842453

5.2、开启Nginx账户认证功能

由 ngx_http_auth_basic_module 模块提供此功能,此模块为默认模块

另外设置账号密码,借助于apache的工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#CentOS安装包:yum -y install httpd-tools
#Ubuntu安装包:apt -y install apache2-utils

[root@Rocky ~]#yum -y install httpd-tools
[root@Rocky ~]#rpm -ql httpd-tools
/usr/bin/ab
/usr/bin/htdbm
/usr/bin/htdigest
/usr/bin/htpasswd #借助此命令创建账号密码
/usr/bin/httxt2dbm
/usr/bin/logresolve
/usr/lib/.build-id
/usr/lib/.build-id/83
/usr/lib/.build-id/83/e9641f1054bcf7ce84912e496b7d7004e541d8
/usr/lib/.build-id/94/c8074f3944d2d827c17b555b26ddd231ce6d40
/usr/lib/.build-id/b8/8f3b9720fa23977b4d7a8de7fb789e300c2bb3
/usr/lib/.build-id/cb
/usr/lib/.build-id/cb/71e4ce4ee4e05e390dc66bdd9b290b78dd595b
/usr/lib/.build-id/ec
/usr/lib/.build-id/ec/f49ee37342186615ecf97b8224c3f74a0b2252
/usr/lib/.build-id/fb
/usr/lib/.build-id/fb/cadf8908e128a1bfc4253b351fc06595960b37
/usr/share/doc/httpd-tools
/usr/share/doc/httpd-tools/LICENSE
/usr/share/doc/httpd-tools/NOTICE
/usr/share/man/man1/ab.1.gz
/usr/share/man/man1/htdbm.1.gz
/usr/share/man/man1/htdigest.1.gz
/usr/share/man/man1/htpasswd.1.gz
/usr/share/man/man1/httxt2dbm.1.gz
/usr/share/man/man1/logresolve.1.gz
[root@Rocky ~]#htpasswd -help #查看用法
htpasswd: illegal option -- h
Usage:
htpasswd [-cimB25dpsDv] [-C cost] [-r rounds] passwordfile username
htpasswd -b[cmB25dpsDv] [-C cost] [-r rounds] passwordfile username password

htpasswd -n[imB25dps] [-C cost] [-r rounds] username
htpasswd -nb[mB25dps] [-C cost] [-r rounds] username password
-c Create a new file.
-n Don't update file; display results on stdout.
-b Use the password from the command line rather than prompting for it.
[root@Rocky ~]#htpasswd -bc /apps/nginx/conf.d/.nginx-user zhang 123456
Adding password for user zhang
# 存放路径 账号 密码
[root@Rocky ~]#cat /apps/nginx/conf.d/.nginx-user
zhang:$apr1$xstp1TOk$IRjBaDIckZXRt3hVdQgBW0
#创建第二个账号时,命令为htpasswd -b /apps/nginx/conf.d/.nginx-user 账号 密码。如果+c是替换旧账号,不+c是增加新账号

#进行安全加固
[root@Rocky ~]#chown nginx.nginx /apps/nginx/conf.d/.nginx-user
[root@Rocky ~]#chmod 600 /apps/nginx/conf.d/.nginx-user
[root@Rocky ~]#vim /apps/nginx/conf.d/pc.conf
location /nginx_status {
stub_status;
auth_basic "waring"; #waring只是提示词,可以随意写
auth_basic_user_file /apps/nginx/conf.d/.nginx-user;
}
:wq
#注意auth_basic和auth_basic_user_file可以放在http(所有网站)、server(其中一个网站)、location(网站中的1个页面)
[root@Rocky ~]#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@Rocky ~]#nginx -s reload

image-20221128155842696

5.3、Nginx 四层访问控制

访问控制基于模块ngx_http_access_module(默认模块)实现,可以通过匹配客户端源IP地址进行限制

注意: 如果能在防火墙设备控制,最好就不要在nginx上配置,可以更好的节约资源

官方帮助:

1
http://nginx.org/en/docs/http/ngx_http_access_module.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@Rocky ~]#vim /apps/nginx/conf.d/pc.conf
location /nginx_status {
stub_status;
auth_basic "waring";
auth_basic_user_file /apps/nginx/conf.d/.nginx-user;
deny 10.0.0.185; #拒绝10.0.0.185访问
allow 10.0.0.0/24; #允许10.0.0.0/24访问,另外允许与拒绝按上下顺序执行
}
:wq
#注意allow和deny可以放在http(所有网站)、server(其中一个网站)、location(网站中的1个页面)
[root@Rocky ~]#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@Rocky ~]#nginx -s reload

#10.0.0.185机器访问
[root@client ~]#curl http://zhang:123456@www.magedu.org/nginx_status
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
#10.0.0.186机器访问
[root@Rocky ~]#curl http://zhang:123456@www.magedu.org/nginx_status
Active connections: 1
server accepts handled requests
46 46 50
Reading: 0 Writing: 1 Waiting: 0

六、安装第三方echo模块实现信息显示

开源的echo模块可以用来打印信息,变量等

1
https://github.com/openresty/echo-nginx-module

image-20221128163638945

6.1、安装echo模块

1
2
3
4
5
6
7
8
9
10
11
[root@Rocky ~]#cd /usr/local/src
[root@Rocky src]#wget https://github.com/openresty/echo-nginx-module/archive/refs/tags/v0.63.tar.gz
[root@Rocky src]#ls
nginx-1.20.2 nginx-1.20.2.tar.gz nginx-module-vts-0.2.1 v0.2.1.tar.gz v0.63.tar.gz
[root@Rocky src]#tar xf v0.63.tar.gz
[root@Rocky src]#ls
echo-nginx-module-0.63 nginx-1.20.2 nginx-1.20.2.tar.gz nginx-module-vts-0.2.1 v0.2.1.tar.gz v0.63.tar.gz
[root@Rocky nginx-1.20.2]#cd nginx-1.20.2/
[root@Rocky 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 --add-module=/usr/local/src/echo-nginx-module-0.63
[root@Rocky nginx-1.20.2]#make -j 2 && make install
[root@Rocky nginx-1.20.2]#systemctl restart nginx

6.2、设置配置文件

Nginx里的变量

1
http://nginx.org/en/docs/varindex.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@Rocky ~]#vim /apps/nginx/conf.d/pc.conf
location /echo {
set $class n68; #可以自定义变量,使用在server和location
echo $class; #打印自定义变量
echo "hello"; #可以打印字符串
echo $remote_addr; #可以打印变量,用户地址
echo $uri; #可以打印变量,访问的地址是什么
}
:wq
[root@Rocky ~]#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@Rocky ~]#nginx -s reload

[root@client ~]#curl http://www.magedu.org/echo
n68
hello
10.0.0.185
/echo

七、nginx的反向代理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
环境配置:
10.0.0.184 为反向代理服务器
10.0.0.185 为后端服务器

#配置反向代理服务器
[root@server conf.d]#vim pc.conf
server {
listen 80;
server_name www.magedu.org;
root /data/nginx/html/pc;
access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json;
location / {
proxy_pass http://10.0.0.185; #添加此项即可开启反向代理服务
}
}
:wq
[root@server conf.d]#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@server conf.d]#nginx -s reload

#nginx的方向代理时,后端服务器只能看到访问地址是方向代理服务器,看不到客户端地址(反向代理服务器可以看到客户端地址)
[root@Rocky nginx]#tail -f logs/access.log #后端服务器
10.0.0.184 - - [29/Nov/2022:13:20:49 +0800] "GET / HTTP/1.0" 200 607 "-" "curl/7.61.1"

image-20221129131337835

八、nginx的负载均衡

Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能

1
https://nginx.org/en/docs/http/ngx_http_upstream_module.html

8.1、http upstream配置参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#自定义一组服务器,配置在http块内
upstream name {
server .....
......
}

#示例
upstream backend {
  server backend1.example.com weight=5;
  server 127.0.0.1:8080  max_fails=3 fail_timeout=30s;
  server unix:/tmp/backend3;
  server backup1.example.com backup;
}

server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number  #给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number  #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测
多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性
检查,而非周期性的探测
fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次
进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
backup  #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器
down    #标记为down状态,可以平滑下线后端服务器,新用户不再调度到此主机,旧用户不受影响

8.2、负载均衡参数设置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@proxy ~]#cd /apps/nginx/conf.d
[root@proxy conf.d]#vim pc.conf
upstream webserver { #添加upstream,后端服务器有下面两台
server 10.0.0.185;
server 10.0.0.186;
}
server {
listen 80;
server_name www.magedu.org;
access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json;
root /data/nginx/html/pc;
location / {
proxy_pass http://webserver; #反向代理功能,代理的地址webserver与负载均衡功能upstream设置的名字一致
}
}
:wq
[root@proxy conf.d]#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@proxy conf.d]#nginx -s reload
#注意此时负载均衡采用的轮询算法(即默认算法)

九、实现nginx四层负载均衡

Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似,其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能。

如果编译安装,需要指定 –with-stream 选项才能支持ngx_stream_proxy_module模块

1
2
3
官方文档:
https://nginx.org/en/docs/stream/ngx_stream_proxy_module.html
http://nginx.org/en/docs/stream/ngx_stream_upstream_module.html

9.1、tcp负载均衡配置参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#stream语句块放在main里面,与http语句块同级
stream {
  upstream backend { #定义后端服务器
      hash $remote_addr consistent; #定义调度算法
      server backend1.example.com:12345 weight=5; #定义具体server
      server 127.0.0.1:12345      max_fails=3 fail_timeout=30s;
      server unix:/tmp/backend3;
  }
  upstream dns {  #定义后端服务器
      server 10.0.0.1:53535;  #定义具体server
      server dns.example.com:53;
  }
  server { #定义server
      listen 12345; #监听IP:PORT
      proxy_connect_timeout 1s; #连接超时时间
      proxy_timeout 3s; #转发超时时间
      proxy_pass backend; #转发到具体服务器组
  }
  server {
      listen 127.0.0.1:53 udp reuseport;
      proxy_timeout 20s;
      proxy_pass dns;
  }
  server {
      listen [::1]:12345;
      proxy_pass unix:/tmp/stream.socket;
  }
}

9.2、实现负载均衡(redis和mysql类似)

image-20221130102253614

9.2.1、后端服务器安装redis
1
2
3
4
5
6
#安装两台redis服务器
[root@centos8 ~]# yum -y install redis
[root@centos8 ~]# sed -i '/^bind /c bind 0.0.0.0' /etc/redis.conf
[root@centos8 ~]# systemctl enable --now redis
[root@centos8 ~]# ss -tnl | grep 6379
LISTEN     0      128         *:6379                     *:*
9.2.2、反向代理nginx配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@centos8 ~]# vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf/tcp/tcp.conf; #注意此处的include与http模块平级
[root@centos8 ~]# mkdir /apps/nginx/conf/tcp
[root@centos8 ~]# cat /apps/nginx/conf/tcp/tcp.conf #也可直接在总配置文件里编辑
stream {
upstream redis_server {
   #hash $remote_addr consistent;
  server 10.0.0.18:6379 max_fails=3 fail_timeout=30s;
  server 10.0.0.28:6379 max_fails=3 fail_timeout=30s;
}
server {
  listen 10.0.0.8:6379; #可直接写listen 6379;
  proxy_connect_timeout 3s;
  proxy_timeout 3s;
  proxy_pass redis_server;
}
}
#重启nginx并访问测试
[root@centos8 ~]# systemctl restart nginx
[root@centos8 ~]# ss -tnl | grep 6379
LISTEN     0      128    10.0.0.8:6379                     *:*  
#测试通过nginx 负载连接redis:
[root@centos8 ~]#redis-cli -h 10.0.0.8 set name wang
OK
[root@centos8 ~]#redis-cli -h 10.0.0.8 get name
(nil)
[root@centos8 ~]#redis-cli -h 10.0.0.8 get name
"wang"

十、实现FastCGI(异构代理)

前面的代理都属于同构代理

image-20221130103145411

nginx缺通过与第三方基于协议实现,即通过某种特定协议将客户端请求转发给第三方服务处理,第三方服务器会新建新的进程处理用户的请求,处理完成后返回数据给Nginx并回收进程,最后nginx在返回给客户端,那这个约定就是通用网关接口(common gateway interface,简称CGI),CGI(协议) 是web服务器和外部应用程序之间的接口标准,是cgi程序和web服务器之间传递信息的标准化接口。

Nginx基于模块ngx_http_fastcgi_module实现通过fastcgi协议将指定的客户端请求转发至php-fpm处

理,其配置指令如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fastcgi_pass address;
#转发请求到后端服务器,address为后端的fastcgi server的地址,可用位置:location, if in location

#示例
fastcgi_pass localhost:9000;
fastcgi_pass unix:/tmp/fastcgi.socket;

fastcgi_index name;
#fastcgi默认的主页资源,示例:fastcgi_index index.php;

fastcgi_param parameter value [if_not_empty];
#设置传递给FastCGI服务器的参数值,可以是文本,变量或组合,可用于将Nginx的内置变量赋值给自定义key
fastcgi_param REMOTE_ADDR        $remote_addr; #客户端源IP
fastcgi_param REMOTE_PORT        $remote_port; #客户端源端口
fastcgi_param SERVER_ADDR        $server_addr; #请求的服务器IP地址
fastcgi_param SERVER_PORT        $server_port; #请求的服务器端口
fastcgi_param SERVER_NAME        $server_name; #请求的server name

Nginx默认配置示例:
  location ~ \.php$ {
    root           /scripts;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name; #默认脚
本路径
     #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #此行写法不再需要上面的 root 指令
    include       fastcgi_params;    #此文件默认系统已提供,存放的相对路径为prefix/conf
  }

10.1、 FastCGI实战案例: Nginx与php不在同一个服务器

nginx会处理静态请求,但是会转发动态请求到后端指定的php-fpm服务器,因此php代码需要放在后端

的php-fpm服务器,即静态页面放在Nginx服务器上,而动态页面放在后端php-fpm服务器,通常情况

下,一般都是采用在同一个服务器

image-20221130105907996

10.1.1、php-fpm的配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
[root@php-fpm ~]#yum -y install php-fpm            #安装软件
[root@php-fpm ~]#rpm -ql php-fpm #查看软件的配置
/etc/httpd/conf.d/php.conf
/etc/logrotate.d/php-fpm
/etc/nginx/conf.d/php-fpm.conf
/etc/nginx/default.d/php.conf
/etc/php-fpm.conf #主配置文件
/etc/php-fpm.d
/etc/php-fpm.d/www.conf #子配置文件
/etc/systemd/system/php-fpm.service.d
/run/php-fpm
/usr/lib/.build-id
/usr/lib/.build-id/eb
/usr/lib/.build-id/eb/2f5f408539a6608c445913b3214d215b421869
/usr/lib/systemd/system/httpd.service.d/php-fpm.conf
/usr/lib/systemd/system/nginx.service.d/php-fpm.conf
/usr/lib/systemd/system/php-fpm.service #有service文件,可以使用system功能
/usr/sbin/php-fpm
/usr/share/doc/php-fpm
/usr/share/doc/php-fpm/php-fpm.conf.default
/usr/share/doc/php-fpm/www.conf.default
/usr/share/fpm
/usr/share/fpm/status.html
/usr/share/licenses/php-fpm
/usr/share/licenses/php-fpm/fpm_LICENSE
/usr/share/man/man8/php-fpm.8.gz
/var/lib/php/opcache
/var/lib/php/session
/var/lib/php/wsdlcache
/var/log/php-fpm
#php-fpm的监听端口是9000
[root@php-fpm ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@php-fpm ~]#systemctl enable --now php-fpm #启动服务
[root@php-fpm ~]#ps auxf | grep php-fpm #此时可以看到php-fpm以Apache账号启动了
root 1870 0.0 0.9 166940 17744 ? Ss 11:06 0:00 php-fpm: master process (/etc/php-fpm.conf)
apache 1871 0.0 0.5 183288 9896 ? S 11:06 0:00 \_ php-fpm: pool www
apache 1872 0.0 0.5 183288 10208 ? S 11:06 0:00 \_ php-fpm: pool www
apache 1873 0.0 0.5 183288 10208 ? S 11:06 0:00 \_ php-fpm: pool www
apache 1874 0.0 0.5 183288 9824 ? S 11:06 0:00 \_ php-fpm: pool www
apache 1875 0.0 0.5 183288 10208 ? S 11:06 0:00 \_ php-fpm: pool www
#为了便于统一管理,或者后期nginx和php-fpm合为一台机器上时账号统一,需更改账号
[root@php-fpm ~]#vim /etc/php-fpm.d/www.conf
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx #将Apache改为nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx #将Apache改为nginx
[root@php-fpm ~]#systemctl restart php-fpm #重新启动,此时账号就改为nginx了

#但此时仍没有9000端口
[root@php-fpm ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
#因为此软件默认以文件的方式监听(即只能在本机访问,不能跨网络访问),跨网络访问需更改配置
[root@php-fpm ~]#vim /etc/php-fpm.d/www.conf
; Note: This value is mandatory.
;listen = /run/php-fpm/www.sock #添加;注释此行
listen = 9000 #添加此行
[root@php-fpm ~]#systemctl restart php-fpm
[root@php-fpm ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 *:9000 *:*
#但配置文件默认权限是本机127.0.0.1,需更改否则跨网络9000不起作用
[root@php-fpm ~]#vim /etc/php-fpm.d/www.conf
;Default Value: any #此行默认都能访问
;listen.allowed_clients = 127.0.0.1 #添加;注释此行
[root@php-fpm ~]#systemctl restart php-fpm

#创建存放PHP程序的存放路径
[root@php-fpm ~]#mkdir /data/php -p
[root@php-fpm ~]#cd /data/php
[root@php-fpm php]#vim test.php
<?php
phpinfo();
?>
:wq
10.1.2、反向代理nginx的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@proxy conf.d]#vim pc.conf
server {
listen 80;
server_name www.magedu.org;
access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json;
root /data/nginx/html/pc;
location / {
proxy_pass http://webserver;
}
location ~ \.php$ {
root /data/php; #php的存放路径
fastcgi_pass 10.0.0.185:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #$document_root表示/data/php,$fastcgi_script_name表示/data/php下的某一文件名称
include fastcgi_params; #系统固定设置,不变
}
}
:wq
[root@proxy conf.d]#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@proxy conf.d]#nginx -s reload

image-20221130115618073

10.1.3、php-fpm也可以开启状态页和ping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@php-fpm php]#vim /etc/php-fpm.d/www.conf
; Default Value: not set
pm.status_path = /fpm_status #取消此行注释,且便于区分,将status改为fpm_status
.....
; Default Value: not set
ping.path = /ping #取消此行注释
....
; Default Value: pong
ping.response = pong #取消此行注释
:wq
[root@php-fpm php]#systemctl restart php-fpm

[root@proxy conf.d]#vim pc.conf
server {
......
location ~ ^/(ping|fpm_status)$ {
fastcgi_pass 10.0.0.185:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@proxy conf.d]#nginx -s reload

image-20221130122042043

image-20221130122106468

十一、项目实战:利用LNMP实现WordPress站点搭建

image-20221130141246807

10.1、LNP的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#此前已安装nginx
[root@LNP ~]#yum -y install php-fpm php-json php-mysqlnd

#先修改php-fpm配置文件
[root@LNP ~]#rpm -ql php-fpm
/etc/httpd/conf.d/php.conf
/etc/logrotate.d/php-fpm
/etc/nginx/conf.d/php-fpm.conf
/etc/nginx/default.d/php.conf
/etc/php-fpm.conf
/etc/php-fpm.d
/etc/php-fpm.d/www.conf
/etc/systemd/system/php-fpm.service.d
/run/php-fpm
/usr/lib/.build-id
/usr/lib/.build-id/eb
/usr/lib/.build-id/eb/2f5f408539a6608c445913b3214d215b421869
/usr/lib/systemd/system/httpd.service.d/php-fpm.conf
/usr/lib/systemd/system/nginx.service.d/php-fpm.conf
/usr/lib/systemd/system/php-fpm.service
/usr/sbin/php-fpm
/usr/share/doc/php-fpm
/usr/share/doc/php-fpm/php-fpm.conf.default
/usr/share/doc/php-fpm/www.conf.default
/usr/share/fpm
/usr/share/fpm/status.html
/usr/share/licenses/php-fpm
/usr/share/licenses/php-fpm/fpm_LICENSE
/usr/share/man/man8/php-fpm.8.gz
/var/lib/php/opcache
/var/lib/php/session
/var/lib/php/wsdlcache
/var/log/php-fpm
[root@LNP ~]#vim /etc/php-fpm.d/www.conf
; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx #将Apache改为nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx #将Apache改为nginx
; listen = /run/php-fpm/www.sock #将此行注释
listen = 127.0.0.1:9000 #添加此行
.....
; Default Value: not set
pm.status_path = /fpm_status #取消此行注释,且便于区分,将status改为fpm_status
.....
; Default Value: not set
ping.path = /ping #取消此行注释
....
; Default Value: pong
ping.response = pong #取消此行注释
:wq
[root@LNP ~]#systemctl enable --now php-fpm

#设置nginx的配置
[root@LNP ~]#vim /apps/nginx/conf.d/pc.conf
server {
listen 80;
server_name www.magedu.org;
access_log /apps/nginx/logs/access-json-www.magedu.org.log access_json;
root /data/nginx/html/pc;
location / {
#root /data/nginx/html/pc;
proxy_pass http://webserver;
}
location ~ \.php$ {
root /data/nginx/html/pc;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ ^/(ping|fpm_status)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
:wq
[root@LNP ~]#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@LNP ~]#nginx -s reload

#下载wordpress
[root@LNP ~]#wget https://cn.wordpress.org/latest-zh_CN.tar.gz
[root@LNP ~]#ls
anaconda-ks.cfg install_nginx.sh latest-zh_CN.tar.gz
[root@LNP ~]#tar xf latest-zh_CN.tar.gz
[root@LNP ~]#ls
anaconda-ks.cfg install_nginx.sh latest-zh_CN.tar.gz wordpress
[root@LNP ~]#mv wordpress/* /data/nginx/html/pc
[root@LNP ~]#chown -R nginx. /data/nginx/html/pc

10.2、数据库mysql的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@Rocky ~]#yum -y install mysql-server
[root@Rocky ~]#systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@Rocky ~]#mysql
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)

mysql> create user wordpress@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all on wordpress.* to wordpress@'10.0.0.%';
Query OK, 0 rows affected (0.00 sec)

十二、


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!