实验简介
一、Nginx 下构建 PC 站点实验
实验目的
掌握 root 和 alias 指令的使用,实现基于 Nginx 的站点目录映射,搭建基础 PC 站点。
核心操作与结论
- root 指令(路径拼接) :
- 语法:
location /path { root /dir; } - 逻辑:最终访问路径 =
root指定目录 +location路径;例如location /lee { root /webdata/nginx/timinglee.org/lee/html; },访问lee.timinglee.org/lee/实际对应/webdata/nginx/timinglee.org/lee/html/lee/目录。
- 语法:
- alias 指令(路径替换) :
- 语法:
location /path { alias /file/dir; } - 逻辑:直接将
location路径替换为alias指定的文件 / 目录;例如:location /passwd { alias /etc/passwd; }:访问/passwd直接返回/etc/passwd文件内容;location /passwd/ { alias /mnt/; }:访问/passwd/对应/mnt/目录。
- 语法:
核心区别
root 是 "拼接路径",alias 是 "替换路径";alias 匹配目录时末尾需加 /,否则可能匹配异常。
二、KeepAlived 长链接优化实验
实验目的
配置 Nginx 长连接参数,控制长连接的超时时间和最大请求次数,优化 TCP 连接复用效率。
核心操作与结论
- 设置长链接超时时间(keepalive_timeout) :
- 配置:
keepalive_timeout 5;(单位:秒) - 效果:客户端与 Nginx 建立的长连接,若 5 秒内无新请求,连接自动关闭;通过
telnet测试可验证连接超时断开的行为。
- 配置:
- 设置长链接最大请求次数(keepalive_requests) :
- 配置:
keepalive_requests 3; - 效果:单个长连接最多处理 3 次请求,第 3 次请求响应后,Nginx 主动关闭连接(响应头
Connection: close);超过次数后客户端需重新建立连接。
- 配置:
核心价值
减少 TCP 三次握手 / 四次挥手的开销,提升高并发场景下的服务响应效率。
三、Location 字符匹配实验
实验目的
验证 Nginx 中 location 指令不同匹配规则的优先级、匹配逻辑,掌握各类匹配符号的使用场景。
核心操作与结论
- 无符号直接匹配 :
- 语法:
location /path - 逻辑:前缀匹配(区分大小写),仅匹配以指定路径开头的请求,但优先级最低;例如
/null仅匹配lee.timinglee.org/null/,不匹配/NULL/或/test/null。
- 语法:
- =` 精确匹配 :
- 语法:
location = /path - 逻辑:完全匹配指定路径,优先级最高;例如
location = /null会优先匹配lee.timinglee.org/null,覆盖其他/null相关匹配规则。
- 语法:
- ^~ 前缀匹配(跳过正则) :
- 语法:
location ^~ /path - 逻辑:前缀匹配,优先级高于正则匹配;匹配以指定路径开头的请求(如
/lee匹配/lee/test、/leeab/test),且匹配成功后不再执行正则匹配。
- 语法:
- ~ 正则匹配(区分大小写) :
- 语法:
location ~ /pattern - 逻辑:基于正则表达式匹配,区分大小写;例如
~ /timing/匹配包含/timing/的路径(如/timinga/、/a/timing/),但不匹配/Timinga/。
- 语法:
- *~ 正则匹配(不区分大小写)**:
- 语法:
location ~* /pattern - 逻辑:正则匹配且忽略大小写;例如
~* /timinglee可匹配/Timinglee/、/a/Timinglee/a等路径。
- 语法:
- \ 转义符 + 正则匹配文件后缀 :
- 语法:
location ~* \.(img|php|jsp)$ - 逻辑:匹配以指定后缀结尾的请求,
\用于转义.(正则中.为通配符),例如匹配/test.php、/test.jsp。
- 语法:
匹配优先级总结
= 精确匹配 > ^~ 前缀匹配 > ~/~* 正则匹配 > 无符号前缀匹配。
四、服务访问的用户认证实验
实验目的
为 Nginx 特定路径添加 HTTP 基本认证,限制未授权访问,提升站点安全性。
核心操作与结论
-
生成密码文件 :
- 使用
htpasswd工具创建用户密码文件:htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin lee(-c创建文件,-mmd5 加密,-b直接指定密码)。
- 使用
-
配置认证指令 :
-
在
location /admin中添加:auth_basic "login passwd"; # 认证提示语 auth_basic_user_file "/usr/local/nginx/conf/.htpasswd"; # 密码文件路径
-
-
测试验证 :
- 未携带认证信息访问
/admin返回 401 错误; - 通过
curl -uadmin:lee http://lee.timinglee.org/admin/携带账号密码可正常访问。
- 未携带认证信息访问
五、自定义错误页面实验
实验目的
替换 Nginx 默认的 404/405/502/503 等错误页面,返回自定义提示信息,优化用户体验。
核心操作与结论
- 创建自定义错误内容文件 :
- 新建目录
/usr/local/nginx/errorpage,写入自定义提示(如 "太不巧了,你要访问的页面辞职了!!")。
- 新建目录
- 配置 error_page 指令 :
- 语法:
error_page 404 405 503 502 /error;(将指定错误码重定向到/error路径) - 配合
location /error+alias指令,将/error路径映射到自定义错误文件;例如访问不存在的/lee/路径时,返回自定义提示而非 Nginx 默认 404 页面。
- 语法:
关键注意点
alias 用于精确映射文件 / 目录(与 root 不同:root 是拼接路径,alias 是替换路径),此处 alias /usr/local/nginx/errorpage/errormessage 直接指向错误提示文件。
六、自定义错误日志
该实验旨在为 Nginx 服务器中指定域名lee.timinglee.org配置自定义错误日志并验证效果:
- 先创建专属日志目录
/usr/local/nginx/logs/timinglee.org/,再编辑 Nginx 虚拟主机配置文件,为该域名设置 80 端口监听、指定 404/405/503/502 错误跳转至/error路径,同时配置独立的错误日志文件logs/timinglee.org/lee.error(仅记录 error 级别日志),并定义/lee和/error路径对应的文件目录; - 重启 Nginx 使配置生效后,测试访问
lee.timinglee.org/lee/触发 404 错误,不仅返回自定义错误提示,且错误详情(文件不存在)被成功写入专属的lee.error日志文件,实现了域名错误日志隔离与自定义错误页展示。
Nginx下构建PC站点
location中的root
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# mkdir conf.d
[root@nginx conf]# vim nginx.conf
82 include "/usr/local/nginx/conf/conf.d/*.conf";
[root@nginx conf]# nginx -s reload
[root@nginx ~]# mkdir -p /webdata/nginx/timinglee.org/lee/html
[root@nginx ~]# echo lee.timinglee.org > /webdata/nginx/timinglee.org/lee/html/index.html
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# cd conf.d/
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
}
[root@nginx conf.d]# systemctl restart nginx.service
#测试
[root@nginx conf.d]# vim /etc/hosts
172.25.254.100 Nginx www.timinglee.org lee.timinglee.org
[root@nginx conf.d]# curl www.timinglee.org
timinglee
[root@nginx conf.d]# curl lee.timinglee.org
lee.timinglee.org
#local示例需要访问lee.timinglee.org/lee/目录
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location / {
root /webdata/nginx/timinglee.org/lee/html;
}
location /lee { #lee标识location中的root值+location 后面指定的值代表目录的路径
root /webdata/nginx/timinglee.org/lee/html;
}
}
[root@nginx conf.d]# systemctl restart nginx.service
[root@nginx conf.d]# mkdir -p /webdata/nginx/timinglee.org/lee/html/lee
[root@nginx conf.d]# echo lee > /webdata/nginx/timinglee.org/lee/html/lee/index.html
[root@nginx conf.d]# curl lee.timinglee.org/lee/
lee
location中的alias
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /passwd { #标识文件
alias /etc/passwd;
}
location /passwd/ { #表示目录
alias /mnt/;
}
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# echo passwd > /mnt/index.html
[root@nginx conf.d]# curl lee.timinglee.org/passwd/
passwd
[root@nginx conf.d]# curl lee.timinglee.org/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
KeepAlived长链接优化
设定长链接时间
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# nginx -s reload
#测试
[root@nginx ~]# dnf install telnet -y
[root@nginx ~]# telnet www.timinglee.org 80
Trying 172.25.254.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK
Server: nginx/1.28.1
Date: Tue, 10 Feb 2026 08:38:50 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 10 Feb 2026 08:23:57 GMT
Connection: keep-alive
ETag: "698aeb1d-a"
Accept-Ranges: bytes
timinglee 显示的页面出现后根据设定的长链接时间会等待,超过时间后会自动退出
Connection closed by foreign host.
设定长链接次数
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
[root@nginx ~]# nginx -s reload
[root@nginx ~]# telnet www.timinglee.org 80
Trying 172.25.254.100...
Connected to www.timinglee.org.
Escape character is '^]'.
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK #第一次
Server: nginx/1.28.1
Date: Tue, 10 Feb 2026 08:42:11 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 10 Feb 2026 08:23:57 GMT
Connection: keep-alive
ETag: "698aeb1d-a"
Accept-Ranges: bytes
timinglee
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK #第二次
Server: nginx/1.28.1
Date: Tue, 10 Feb 2026 08:42:20 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 10 Feb 2026 08:23:57 GMT
Connection: keep-alive
ETag: "698aeb1d-a"
Accept-Ranges: bytes
timinglee
GET / HTTP/1.1
Host: www.timinglee.org
HTTP/1.1 200 OK #第三次
Server: nginx/1.28.1
Date: Tue, 10 Feb 2026 08:42:30 GMT
Content-Type: text/html
Content-Length: 10
Last-Modified: Tue, 10 Feb 2026 08:23:57 GMT
Connection: close
ETag: "698aeb1d-a"
Accept-Ranges: bytes
timinglee
Connection closed by foreign host.
Location 字符匹配详解
Location后什么都不带直接指定目录
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# cd conf.d/
[root@nginx conf.d]# vim vhosts.conf
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl lee.timinglee.org/null/
/null-1
[root@nginx conf.d]# curl lee.timinglee.org/NULL/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@nginx conf.d]# curl lee.timinglee.org/test/null
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
location 后用 =
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null { #精确匹配到此结束
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl lee.timinglee.org/null
null-2
location 后用"^~"
[root@Nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
}
[root@Nginx conf.d]# nginx -s reload
lee
[root@Nginx conf.d]# curl lee.timinglee.org/lee
lee
[root@Nginx conf.d]# curl lee.timinglee.org/test/lee
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/lee/test
lee
[root@Nginx conf.d]# curl lee.timinglee.org/aleea/test
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx conf.d]# curl lee.timinglee.org/leeab/test
lee
location 后用"~"
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
location ~ /timing/ {
return 200 "timing";
}
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl lee.timinglee.org/timinga/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@nginx conf.d]# curl lee.timinglee.org/timing/
timing
[root@nginx conf.d]# curl lee.timinglee.org/a/timing/
timing
[root@nginx conf.d]# curl lee.timinglee.org/a/timinga/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@nginx conf.d]# curl lee.timinglee.org/a/atiming/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@nginx conf.d]# curl lee.timinglee.org/aTiminga/a/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@nginx conf.d]# curl lee.timinglee.org/Timinga/a/
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
location 后用"~*"
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
location ~ /timing/ {
return 200 "timing";
}
location ~* /timinglee {
return 200 "timinglee";
}
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl lee.timinglee.org/Timinglee/
timinglee
[root@nginx conf.d]# curl lee.timinglee.org/timinglee/
timinglee
[root@nginx conf.d]# curl lee.timinglee.org/timinglee/a
timinglee
[root@nginx conf.d]# curl lee.timinglee.org/a/timinglee/a
timinglee
[root@nginx conf.d]# curl lee.timinglee.org/a/atiminglee/a
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@nginx conf.d]# curl lee.timinglee.org/a/timingleea/a
timinglee
[root@nginx conf.d]# curl lee.timinglee.org/a/Timinglee/a
timinglee
location 后用"\"
[root@nginx conf.d]# vim vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /null {
return 200 "null-1";
}
location = /null {
return 200 "null-2";
}
location ~ /null {
return 200 "null-3";
}
location ^~ /lee {
return 200 "lee";
}
location ~ /timing/ {
return 200 "timing";
}
location ~* /timinglee {
return 200 "timinglee";
}
location ~* \.(img|php|jsp)$ {
return 200 "app";
}
}
[root@nginx conf.d]# nginx -s reload
[root@nginx conf.d]# curl lee.timinglee.org/test.php
app
[root@nginx conf.d]# curl lee.timinglee.org/test.jsp
app
服务访问的用户认证
[root@nginx ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin lee
Adding password for user admin
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /admin {
root /usr/local/nginx/html;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
}
}
[root@nginx ~]# mkdir -p /usr/local/nginx/html/admin
[root@nginx ~]# echo "Welcome to Admin Page (Authenticated)" > /usr/local/nginx/html/admin/index.html
[root@nginx ~]# systemctl restart nginx.service
[root@nginx ~]# curl -uadmin:lee http://lee.timinglee.org/admin/
Welcome to Admin Page (Authenticated)
自定义错误页面
[root@nginx ~]# mkdir /usr/local/nginx/errorpage
[root@nginx ~]# echo "太不巧了,你要访问的页面辞职了!!" > /usr/local/nginx/errorpage/errormessage
[root@nginx ~]# cat /usr/local/nginx/errorpage/errormessage
太不巧了,你要访问的页面辞职了!!
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
}
[root@nginx ~]# systemctl restart nginx.service
[root@nginx ~]# curl lee.timinglee.org/lee/
太不巧了,你要访问的页面辞职了!!
自定义错误日志
[root@nginx ~]# mkdir -p /usr/local/nginx/logs/timinglee.org/
[root@nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
error_page 404 405 503 502 /error;
error_log logs/timinglee.org/lee.error error;
location /lee {
root /usr/local/nginx/html;
}
location /error {
alias /usr/local/nginx/errorpage/errormessage;
}
}
[root@nginx ~]# systemctl restart nginx.service
[root@nginx ~]# cd /usr/local/nginx/logs/timinglee.org/
[root@nginx timinglee.org]# ls
lee.error
[root@nginx timinglee.org]# cat lee.error
[root@nginx timinglee.org]# curl lee.timinglee.org/lee/
太不巧了,你要访问的页面辞职了!!
[root@nginx timinglee.org]# cat lee.error
2026/02/10 17:13:19 [error] 2850#0: *1 "/usr/local/nginx/html/lee/index.html" is not found (2: No such file or directory), client: 172.25.254.100, server: lee.timinglee.org, request: "GET /lee/ HTTP/1.1", host: "lee.timinglee.org"