2026年4月21日
知识点回顾
http部分
网站访问流程:dns->tcp三次握手->http请求->http响应->tcp四次挥手
pv 刷新一个算一个pv
uv 一个设备算一个uv
ip 一个公网算一个pv
nginx部分:
1)安装
1.编译安装
2.yum默认epel仓库安装
3.nginx官方yum仓库安装,官网nginx.org
2)nginx主配置
1.核心区块
启动用户
2.事件区块
最大连接数
3.HTTP区块
server{
}
3)server配置
server {
listen ip:端口;
server_name 域名;
location / {
root 业务目录路径;
index index.html;
}
}
3种配置方式:
1.基于域名方式
server {
listen 80;
server_name www.oldboy.com;
location / {
root 业务目录路径;
index index.html
}
}
2.基于端口方式
server {
listen 81;
server_name _;
location / {
root /code
index index.html
}
}
server{
listen 82;
server_name _;
location / {
root /code;
index index.html;
}
}
3.基于多IP方式
临时添加多个IP
ip add add 10.0.0.7 dev ens3
server {
listen 10.0.0.7:80;
server_name _;
location / {
root /code;
index index.htm;
}
}
server {
listen 10.0.0.8:80
server_name _;
location / {
root /code;
index index.html;
}
}
01.nginx日志:
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
日志位置:
1.访问日志:/var/log/nginx/access.log
2.错误日志:/var/log/nginx/error.log
日志变量含义:
$remote_addr #记录客户端的IP地址
$remote_user #客户端用户名
[$time_local] #本地时间
$request #请求方法 请求资源 请求协议版本号
$status #给用户返回状态码
$body_bytes_sent #资源大小
$http_referer #来源的网站是哪个
$http_user_agent #记录客户端信息
$http_x_forwarded_for #有负载均衡的记录客户端真实的IP地址
02.Nginx模块
模块在哪里找?
nginx.org nginx官网上文档里
-------------------------
1.目录索引模块
2.认证模式auth_basic认证模块
3.访问限制模块 通过IP地址限制
4.Nginx状态模块
5.location的匹配规则 面试题
模块1.开启目录索引
1.开启目录索引
[root@web01 conf.d]# cat server.conf
server {
listen 80;
server_name xbw.oldboy.com;
location /download {
root /code/;
autoindex on; #开启目录索引
}
}
2.创建目录
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
3.浏览器访问 xbw.ym.con/download
4.最终的索引相关的配置
[root@web01 conf.d]# cat server.conf
server {
listen 80;
server_name xbw.oldboy.com;
location /download {
root /code/;
autoindex on; #开启目录索引,不能有index.html
charset utf-8,gbk; #设置字符集
autoindex_exact_size off; #设置人类可读文件大小
autoindex_localtime on; #设置显示本地时间
}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
2.认证模块auth_basic认证模块
1.模块说明:
location / {
auth_basic "closed site"; #描述说明
auth_basic_user_file htpasswd; #密码文件的位置
}
2.生成用户密码:
密码文件需要用命令生成
-b 允许命令行直接指定命令
-c 创建新文件
[root@web01 nginx]# htpasswd -b -c htpasswd test 123
Adding password for user test
[root@web01 nginx]# cat htpasswd
test:$apr1$I5Qc.dGC$XpRaNH/U7QLVtbipNuMPs.
3.配置location
[root@web01 conf.d]# cat server.conf
server {
listen 80;
server_name xbw.oldboy.com;
location /download {
root /code/;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "closed site"; #描述说明
auth_basic_user_file htpasswd; #密码文件的位置
}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
4.测试访问xbw.ym.com/download 需要输入用户名test密码123
3.访问限制模块 通过IP地址限制
1.配置
[root@web01 conf.d]# cat server.conf
server {
listen 80;
server_name xbw.oldboy.com;
location /download {
root /code/;
autoindex on;
charset utf-8,gbk;
autoindex_exact_size off;
autoindex_localtime on;
auth_basic "closed site";
auth_basic_user_file htpasswd;
allow 10.0.0.7; #只允许10.0.0.7
deny all; #拒绝所有
}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
2.测试访问xbw.ym.com
4.Nginx状态模块
1.开启状态模块
[root@web01 conf.d]# cat server.conf
server {
listen 80;
server_name xbw.ym.com;
location / {
root /code/xbw;
index index.html;
}
location /nginx_status {
stub_status; #开启状态模块信息
}
}
2.访问测试
http://xbw.ym.com/nginx_status
Active connections: 2
server accepts handled requests
2 2 24
Reading: 0 Writing: 1 Waiting: 1
Active connections #当前活动的连接数
accepts #已接收TCP的总TCP连接数量
handled #已处理的TCP连接数量
requests #当前http请求数
Reading #当前读取请求头数量
Writing #当前响应的请求头数量
Waiting #等待的请求数,开启了keepalive
#注意,一次TCP的连接,可以发起多次http的请求,如下参数可配置进行验证
keepalive_timeout 0; # 类似于关闭长连接
keepalive_timeout 65; # 65s没有活动则断开连接
5.location的匹配规则 面试题
| 匹配符 | 匹配规则 | 优先级 |
|---|---|---|
| = | 精确匹配 | 1 |
| ^~ | 以某个字符串 | 2 |
| ~ | 区分大小写的正则匹配 | 3 |
| ~* | 不区分大小写的正则匹配 | 4 |
| / | 通用匹配,任何请求都会匹配到 | 5 |
测试验证:
[root@web01 conf.d]# cat test.conf
server {
listen 80;
default_type text/html;
server_name dazi.ym.com;
location = / {
return 200 "[ configuration A ]";
}
location / {
return 200 "[ configuration B ]";
}
location /documents/ {
return 200 "[ configuration C ]";
}
location ^~ /images/ {
return 200 "[ configuration D ]";
}
location ~* \.(gif|jpg|jpeg)$ {
return 200 "[ configuration E ]";
}
}
The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.
---------------------
测试:
[root@web01 conf.d]# curl dazi.ym.com/
[ configuration A ]
[root@web01 conf.d]# curl dazi.ym.com/index.html
[ configuration B ]
....
03.LNMP架构:
含义:
L: Linux
N: Nginx
M: MySQL
P: PHP
LNMT: Tomcat
LNMP: Python
部署LNMP架构:
1.部署Nginx
2.部署PHP
3.部署MySQL
1.部署Nginx
前面案例已部署
2.部署php
2.1.安装php服务
[root@web01 ~]# yum -y install php php-bcmath php-cli php-common php-devel php-embedded php-fpm php-gd php-intl php-mbstring php-mysqlnd php-opcache php-pdo php-process php-xml php-json
[root@web01 ~]# rpm -qa|grep php|wc -l
15
2.2.修改php服务配置
[root@web01 ~]# grep "listen =" /etc/php-fpm.d/www.conf -n
38:listen = 127.0.0.1:9000
2.3.启动php服务
[root@web01 ~]# php-fpm -t #语法检查
[21-Apr-2026 17:19:31] NOTICE: configuration file /etc/php-fpm.conf test is successful
#启动
[root@web01 ~]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
#查看端口9000
[root@web01 ~]# netstat -tnulp|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 10505/php-fpm: mast
3.部署MySQL服务
3.1.安装数据库
[root@web01 ~]# yum -y install mariadb-server
[root@web01 ~]# systemctl start mariadb
[root@web01 ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
3.2.配置密码
[root@web01 ~]# mysqladmin password 'lzy123.com'
3.3.登录测试
[root@web01 ~]# mysql -uroot -p'lzy123.com'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.39-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit #退出
Bye
04.将Nginx和PHP和mysql进行连接
1.将Nginx和PHP进行打通
1)配置Nginx中集成PHP服务
[root@web01 conf.d]# cat php.conf
server {
listen 80;
server_name php.oldboy.com;
location / {
root /code;
index index.php index.html;
}
location ~ \.php$ {
root /code;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl restart nginx
2)配置index.php 查询php本身信息代码
[root@web01 conf.d]# cat /code/index.php
<?php
phpinfo();
?>
3)hosts解析
10.0.0.7 xbw.ym.com php.oldboy.com
4)测试
php.oldboy.com
2.测试php和数据库是否可以通信
1)编写业务
[root@web01 code]# cat mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "lzy123.com";
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "小哥哥,php可以连接MySQL...";
?>
<img style='width:100%;height:100%;' src=1.png>
2)访问测试
http://php.oldboy.com/mysql.php
正文完