1. ✅修改默认页面返回403vim /etc/nginx/conf.d/default.conf server { listen 80 default_server; server_name localhost; return 403; }2. ✅location对用户请求中请求的URI进行判断location匹配URI写法说明location / { }默认规则其他location规则都失败了location /zbl/ { }匹配路径location ~ 正则 { }支持正则匹配区分大小写location ~* 正则{ }支持正则匹配不区分大小写location 内容精确匹配一般配合error_page指定错误页面location ^~ 内容不是正则匹配字符优先级高用的较少location xxx命名的location内部跳转3. ✅案例01: 部署一个购物网站域名buy.zbl.cn站点目录/app/code/buy/用户首页文件/app/code/buy/后台管理页面/app/code/buy/index.html要求后台只能内网访问172.16.1.0/24网段#修改配置文件 vim /etc/nginx/conf.d/buy.conf server{ listen 80; server_name buy.zbl.cn; root /app/code/buy/; location / { index index.html; } location /admin/ { #allow 10.0.0.1; allow 172.16.1.0/24; deny all; index index.html; } } #检查 nginx -t #重启 systemctl reload nginx #访问 http://buy.zbl.cn/admin/4. ✅案例02: 小鸟飞飞站点缓存加速4.1. 修改配置文件server { listen 80; server_name bird.zbl.cn; root /app/code/bird/; location / { index index.html; } location ~* \.(html|js|css)$ { expires 1d; } location ~* \.(jpg|jpeg|bmp|gif|png)$ { expires max; } }expires 1d : 表示缓存1天expires max : 表示缓存10年4.2. 浏览器f12查看5. ✅nginx日志5.1. ❌错误日志error_log /var/log/nginx/error.log notice; 给每个站点设置独立的错误日志 notice:表示日志级别控制错误日志详细程度推荐级别notice debug, info, notice, warn, error, crit, alert, or emerg 最详细 最不详细5.2. 访问日志5.2.1. 分析统计访问日志ip次数分析状态码数量分析用户使用的UA分析URI访问次数log_format格式nginx中的变量说明$remote_addr客户端ip地址$remote_user远程的用户,nginx 认证功能的用户$time_local[20/Jun/2025:11:58:54 0800]$requesthttp请求起始行请求方法 URI HTTP/1.1$request_method请求方法 GET,PORT,HEAD,DELETE,OPTIONS,PUT$request_uri请求的URI部分$status状态码status code$body_bytes_sent文件的大小(资源的大小)单位是字节$http_referer从哪里跳转过来的$http_user_agentUA头,客户端浏览器代理$http_x_forwarded_for负载均衡中使用,XFF头信息,记录真实IP5.3. 调用access_log /var/log/nginx/access.log main buffer32k flush10s; main:主配置文件中定义的日志格式的名字access_log优化选项access_log /var/log/nginx/access.log main;buffersizebuffer512k; 设置访问日志的缓存flushtimeflush30s; 设置一个日志写入时间gzip对日志进行压缩5.4. 小鸟飞飞日志优化server { listen 8001; server_name bird.zbl.cn; error_log /var/log/nginx/bird.zbl.cn-error.log notice; access_log /var/log/nginx/bird.zbl.cn-access.log main buffer32k flush10s; root /app/code/bird/; location / { index index.html; } location ~* \.(html|js|css)$ { expires 1d; } location ~* \.(jpg|jpeg|bmp|gif|png)$ { expires max; } }buffer32k: 表示缓存32k的日志在内存中再写入日志文件flush10s: 表示每10s写入一次日志作用加速日志写入速度避免频繁写日志6. 配置403/404/500等错误页面404页面(html)/app/code/errors/error_page 404 /404.html; location /404.html { root /app/code/errors/; }6.1. 404.html!DOCTYPE html html langzh-CN head meta charsetUTF-8 / meta nameviewport contentwidthdevice-width, initial-scale1.0/ title404 - 页面未找到/title link hrefhttps://fonts.googleapis.com/css2?familyPoppins:wght400;700displayswap relstylesheet style * { box-sizing: border-box; } body { margin: 0; padding: 0; font-family: Poppins, sans-serif; background: linear-gradient(135deg, #1e3c72, #2a5298); color: #fff; display: flex; align-items: center; justify-content: center; height: 100vh; overflow: hidden; } .background-animation { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: radial-gradient(circle at 2px 2px, rgba(255,255,255,0.1) 2px, transparent 0); background-size: 40px 40px; animation: moveBG 20s linear infinite; z-index: 0; } keyframes moveBG { from { background-position: 0 0; } to { background-position: -100px -100px; } } .container { position: relative; z-index: 1; text-align: center; max-width: 600px; padding: 20px; } .svg-illustration { width: 200px; height: auto; margin-bottom: 30px; opacity: 0.9; } h1 { font-size: 100px; margin: 0; color: #ffdd57; animation: bounce 2s infinite; } h2 { font-size: 28px; margin-top: 10px; margin-bottom: 10px; } p { font-size: 16px; margin-bottom: 30px; line-height: 1.5; opacity: 0.85; } .search-box { display: flex; justify-content: center; margin-bottom: 20px; } .search-box input[typetext] { padding: 10px 15px; font-size: 16px; border: none; outline: none; border-radius: 5px 0 0 5px; width: 240px; } .search-box button { padding: 10px 20px; font-size: 16px; border: none; background-color: #ffdd57; color: #111; cursor: pointer; border-radius: 0 5px 5px 0; transition: background-color 0.3s ease; } .search-box button:hover { background-color: #ffd700; } a.button { display: inline-block; padding: 12px 30px; background-color: #ffffff; color: #1e3c72; text-decoration: none; border-radius: 5px; font-weight: bold; transition: background-color 0.3s ease; } a.button:hover { background-color: #f1f1f1; } keyframes bounce { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-15px); } } media (max-width: 600px) { h1 { font-size: 70px; } .svg-illustration { width: 150px; } .search-box input[typetext] { width: 180px; } } /style /head body div classbackground-animation/div div classcontainer !-- SVG Illustration -- svg classsvg-illustration viewBox0 0 24 24 fillnone xmlnshttp://www.w3.org/2000/svg path dM12 2L2 7L12 12L22 7L12 2Z stroke#fff stroke-width2 fillnone/ path dM2 17L12 22L22 17 stroke#fff stroke-width2 fillnone/ path dM2 12L12 17L22 12 stroke#fff stroke-width2 fillnone/ /svg h1404/h1 h2页面未找到 / Page Not Found/h2 p 抱歉您请求的页面不存在或已被移除。br/ Sorry, the page you requested does not exist or has been removed. /p div classsearch-box input typetext placeholder输入关键词搜索... / button搜索/button /div a href/ classbutton返回首页 / Go Home/a /div /body /html6.2. 403.html!DOCTYPE html html langzh-CN head meta charsetUTF-8 / meta nameviewport contentwidthdevice-width, initial-scale1.0/ title403 - 禁止访问/title link hrefhttps://fonts.googleapis.com/css2?familyInter:wght400;700displayswap relstylesheet style * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: Inter, sans-serif; background: linear-gradient(135deg, #2c3e50, #34495e); color: #fff; display: flex; align-items: center; justify-content: center; height: 100vh; overflow: hidden; } .background-animation { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: radial-gradient(circle at 2px 2px, rgba(255,255,255,0.05) 2px, transparent 0); background-size: 40px 40px; animation: moveBG 20s linear infinite; z-index: 0; } keyframes moveBG { from { background-position: 0 0; } to { background-position: -100px -100px; } } .container { position: relative; z-index: 1; text-align: center; max-width: 600px; padding: 20px; } .icon-lock { font-size: 100px; color: #e74c3c; margin-bottom: 20px; animation: pulse 2s infinite; } h1 { font-size: 80px; margin: 0; color: #ecf0f1; } h2 { font-size: 28px; margin-top: 10px; margin-bottom: 20px; } p { font-size: 16px; margin-bottom: 30px; line-height: 1.5; opacity: 0.85; } a.button { display: inline-block; padding: 12px 30px; background-color: #ffffff; color: #2c3e50; text-decoration: none; border-radius: 5px; font-weight: bold; transition: background-color 0.3s ease; } a.button:hover { background-color: #f1f1f1; } .countdown { font-size: 14px; color: #ccc; margin-top: 10px; display: block; } keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } } media (max-width: 600px) { h1 { font-size: 60px; } .icon-lock { font-size: 70px; } } /style /head body div classbackground-animation/div div classcontainer !-- 锁图标 -- div classicon-lock/div h1403/h1 h2禁止访问 / Forbidden/h2 p 抱歉您没有权限访问此页面。br/ Sorry, you dont have permission to access this page. /p a href/ idredirect-link classbutton返回首页 / Go Home span idcountdown classcountdown/span/a /div script // 设置跳转时间单位秒 let timeLeft 5; const countdownElement document.getElementById(countdown); const redirectLink document.getElementById(redirect-link); function updateCountdown() { if (timeLeft 0) { countdownElement.textContent 将在 timeLeft 秒后跳转; timeLeft--; setTimeout(updateCountdown, 1000); } else { window.location.href redirectLink.getAttribute(href); } } updateCountdown(); /script /body /html6.3. 500.html!DOCTYPE html html langzh-CN head meta charsetUTF-8 / meta nameviewport contentwidthdevice-width, initial-scale1.0/ title500 - 内部服务器错误/title link hrefhttps://fonts.googleapis.com/css2?familyInter:wght400;700displayswap relstylesheet style * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: Inter, sans-serif; background: linear-gradient(135deg, #2c3e50, #4ca1af); color: #fff; display: flex; align-items: center; justify-content: center; height: 100vh; overflow: hidden; } .background-animation { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: radial-gradient(circle at 2px 2px, rgba(255,255,255,0.05) 2px, transparent 0); background-size: 40px 40px; animation: moveBG 20s linear infinite; z-index: 0; } keyframes moveBG { from { background-position: 0 0; } to { background-position: -100px -100px; } } .container { position: relative; z-index: 1; text-align: center; max-width: 600px; padding: 20px; } .icon-server { font-size: 100px; color: #e74c3c; margin-bottom: 20px; animation: pulse 2s infinite; } h1 { font-size: 80px; margin: 0; color: #ecf0f1; } h2 { font-size: 28px; margin-top: 10px; margin-bottom: 20px; } p { font-size: 16px; margin-bottom: 30px; line-height: 1.5; opacity: 0.85; } a.button { display: inline-block; padding: 12px 30px; background-color: #ffffff; color: #2c3e50; text-decoration: none; border-radius: 5px; font-weight: bold; transition: background-color 0.3s ease; } a.button:hover { background-color: #f1f1f1; } keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } } media (max-width: 600px) { h1 { font-size: 60px; } .icon-server { font-size: 70px; } } /style /head body div classbackground-animation/div div classcontainer !-- 服务器图标 -- div classicon-server/div h1500/h1 h2内部服务器错误 / Internal Server Error/h2 p 抱歉服务器遇到了问题正在努力修复。br/ Sorry, something went wrong on our server. Please try again later. /p a href/ classbutton返回首页 / Go Home/a /div /body /html7. ✅总结nginx官网:认证功能nginx处理用户请求的流程location规则访问日志,错误日志(独立站点)错误页面美化