day039-nginx配置补充
文章目录
- 0. 老男孩思想-如何提升能力?
- 1. nginx登录认证功能
- 1.1 创建密码文件
- 1.2 修改子配置文件
- 1.3 重启服务
- 2. nginx处理请求流程
- 3. 配置默认站点
- 4. location 命令
- 5. 案例1-搭建大型直播购物网站
- 5.1 配置本地hosts解析
- 5.2 编写子配置文件
- 5.3 创建相关目录/文件并重启服务
- 5.4 测试
- 6. 案例2-为小鸟飞飞网站设置缓存,将网站中html、js、css结尾的文件缓存1天,图片缓存1小时(浏览器缓存)
- 6.1 修改子配置文件
- 6.2 重启服务并测试
- 7. nginx日志
- 7.1 日志级别
- 7.2 为站点设置错误日志
- 7.3 访问日志
- 7.3.1 nginx变量
- 7.3.2 为站点设置访问日志
- 7.3.3 访问日志优化
- 8. 配置优雅的404页面
- 9. 踩坑记录
- 1. 为啥配置404页面时的location需要用精确匹配“=”,直接写location /404.html为啥会报错?
- **1.1 理想情况下的工作流程**
- **1.2 潜伏的致命隐患(即使文件正常)**
- **隐患 1:路径混淆攻击**
- **隐患 2:目录越权暴露**
- 1.3 精确匹配的防护作用(`location =`)
- 10. 思维导图
0. 老男孩思想-如何提升能力?
- 能力是从工作中获得的
- 人品好、态度佳是提升能力的必要前提条件
- 老男孩思想Linux运维人员的核心能力列表:
核心职场能力 | 描述 |
---|---|
技术能力(标配) | 运维:Linux运维->DBA专项/k8s与微服务->python自动化开发->管理岗 开发:程序员->DBA专项->程序架构师/需求分析师->管理岗 |
做的能力 | 为人处事、交往人脉获得资源、独挡一面解决问题的能力 |
写的能力 | 文案、总结、画图、写项目方案 |
思的能力 | 策划、营销、销售、管理、严谨的逻辑思维能力、提供解决方案的能力 |
听说能力 | 表达沟通、说话、演讲演说、培训能力 |
其他能力 | 自信心、抗压能力、思维能力、理解力、表现力、专注力、坚持力 |
1. nginx登录认证功能
- nginx官方指令列表地址:
[Alphabetical index of directives](https://nginx.org/en/docs/dirindex.html)
1.1 创建密码文件
-
htpasswd建立和更新存储用户名、密码的文本文件, 用于对HTTP用户的basic认证。
- -c:创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容
- -b:使用批处理模式;即从命令行获取密码,而不是提示输入密码
[root@web01 /etc/nginx]# htpasswd -c -b /etc/nginx/pass lidao 996
Adding password for user lidao
[root@web01 /etc/nginx]# htpasswd -b /etc/nginx/pass oldboy 007
Adding password for user oldboy
[root@web01 /etc/nginx]# cat pass
lidao:$apr1$LZ55RX/U$MBZXp4izeDntNbPuzBgcj.
oldboy:$apr1$7dxKt2s0$pl4Ehi93p5YzH1F.0ulMC/
# 修改密码文件所有者
[root@web01 /etc/nginx]# chown nginx:nginx pass
1.2 修改子配置文件
# 登录认证# 提示auth_basic "登录认证:";# 指定密码文件auth_basic_user_file /etc/nginx/pass;
1.3 重启服务
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx.service
- 测试
- curl
- -u 用户名:密码:指定网站登录认证的用户名和密码
2. nginx处理请求流程
3. 配置默认站点
[root@web01 /etc/nginx/conf.d]# cat default.conf
server {listen 80 default_server; # 设置该站点为默认站点server_name localhost;return 403; # 禁止使用ip访问
}
4. location 命令
- 对请求的uri做出判断
location匹配规则 | 说明 |
---|---|
location / | 默认匹配,当其他匹配不符合时,执行该规则 |
location /index.html | uri匹配,按照指定的uri进行匹配 |
location ~ 正则 | 正则匹配uri,区分大小写(perl正则) |
location ~* 正则 | 正则匹配uri,不区分大小写(perl正则) |
location = 内容 | 精准匹配 |
location ^~ 内容 | 不是正则,匹配字符,优先级高,用的较少 |
location @xxx | 命令的location,内部跳转 |
5. 案例1-搭建大型直播购物网站
- 域名:buy.oldboy.cn
- 站点目录:/app/code/buy
- 首页文件:/app/code/buy/index.html
- 后台管理页面:/app/code/buy/admin/index.html
- 要求后台只能内网访问:10.0.0.1,172.16.1.0/24
5.1 配置本地hosts解析
5.2 编写子配置文件
- 访问限制只针对管理页面
[root@web01 /etc/nginx/conf.d]# cat buy.oldboy.cn.conf
server {listen 80;server_name buy.oldboy.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;}
}
5.3 创建相关目录/文件并重启服务
[root@web01 ~]# mkdir -p /app/code/buy/admin/
[root@web01 ~]# echo user web > /app/code/buy/index.html
[root@web01 ~]# echo admin web > /app/code/buy/admin/index.html
[root@web01 ~]# 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 ~]# systemctl reload nginx.service
5.4 测试
6. 案例2-为小鸟飞飞网站设置缓存,将网站中html、js、css结尾的文件缓存1天,图片缓存1小时(浏览器缓存)
- 缓存指令(浏览器缓存)
- expires
- max:10年
- 1d:一天
- 1h:一小时
6.1 修改子配置文件
[root@web01 /etc/nginx/conf.d]# cat bird.oldboy.conf
server {listen 80;server_name bird.oldboy.cn;root /app/code/bird/;location / {index index.html;}# 设置缓存location ~* \.(html|js|css)$ {expires 1d;}location ~* \.(png|jpg|bmp|jpeg|gif)$ {expires 1h;}
}
6.2 重启服务并测试
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx.service
7. nginx日志
常见uri:/favicon.ico
,网站图标文件
7.1 日志级别
debug info notice warn error crit alert emerg
最详细 默认级别 最粗略
7.2 为站点设置错误日志
# 设置错误日志
error_log /var/log/nginx/bird.oldboy.cn-error.log notice;
- 最后重启服务
7.3 访问日志
7.3.1 nginx变量
- 访问日志格式(log_format)只能放在http区域
- nginx支持的变量,官方地址:
[Alphabetical index of variables](https://nginx.org/en/docs/varindex.html)
log_format中的变量 | 说明 |
---|---|
$remote_addr | 客户端ip |
$remote_user | 认证登录的用户名,如果没有密码认证则为空 |
$time_local | 时间日期 |
$request | http请求起始行内容:请求方法、uri、http版本 |
$status | 状态码 |
$body_bytes_sent | 响应的资源大小,单位:字节 |
$http_referer | 从哪里跳转过来的(分析用户行为,百度竞价) |
$http_user_agent | UA头:客户端浏览器标识 |
$http_x_forwarded_for | 负载均衡中,XFF头信息,记录用户真实ip地址 |
- 其他变量
变量 | 说明 |
---|---|
$request_method | 请求方法 |
$request_uri | 请求起始行的uri |
7.3.2 为站点设置访问日志
# 设置访问日志
access_log /var/log/nginx/bird.oldboy.cn-access.log main;
- main是主配置文件中日志格式的标识
7.3.3 访问日志优化
- 默认情况下是实时将请求信息写入访问日志,这样会占用大量io,影响cpu效率
- 可以设置日志缓存
优化命令 | 用法 | 说明 |
---|---|---|
buffer=size | buffer=512k | 设置访问日志的缓存空间大小 |
flush=time | flush=30s | 设置访问日志写入时间;每隔30秒将缓存数据写入磁盘 |
gzip | 对日志进行压缩,减少磁盘占用空间 |
# 设置访问日志
access_log /var/log/nginx/bird.oldboy.cn-access.log main buffer=32k flush=10s;
8. 配置优雅的404页面
- 下载404页面
[root@web01 /app/code/errors]# wget https://volunteer.cdn-go.cn/404/latest/404.html
- 修改子配置文件
- location是精确匹配
# 配置404界面
error_page 404 /404.html;
location = /404.html {root /app/code/errors;
}
- 重启服务,测试
[root@web01 /etc/nginx/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 /etc/nginx/conf.d]# systemctl reload nginx.service
9. 踩坑记录
1. 为啥配置404页面时的location需要用精确匹配“=”,直接写location /404.html为啥会报错?
1.1 理想情况下的工作流程
- 用户访问不存在页面(如
/test.html
) - Nginx 触发
error_page 404 /404.html
→ 内部重定向到/404.html
- 路径匹配规则生效
location /404.html { # 路径匹配root /app/code/errors/;
}
4.成功读取 /app/code/errors/404.html
5.返回自定义404页面(HTTP状态码 404)
1.2 潜伏的致命隐患(即使文件正常)
隐患 1:路径混淆攻击
当访问 /404.html/secret.txt
这类伪造路径时:
- 被错误匹配到
location /404.html
- Nginx 尝试返回:
/app/code/errors/404.html/secret.txt
- 若网站存在此文件 → 意外泄露敏感文件
- 若文件不存在 → 触发递归循环 → 最终返回 500 错误
隐患 2:目录越权暴露
若 /app/code/errors/
目录下存在其他文件:
- 访问
http://域名/404.html/
(末尾带斜杠) - Nginx 可能返回 目录索引列表(如果开启
autoindex
) - 暴露服务器目录结构(安全风险!)
1.3 精确匹配的防护作用(location =
)
location = /404.html { # 精确匹配root /app/code/errors/;
}
- 路径隔离
仅响应 严格等于/404.html
的请求(不处理/404.html/xxx
) - 阻断越权访问
http://域名/404.html/
会被其他location
处理(如返回403) - 彻底消除递归风险
即使自定义404页面丢失,也不会引发循环(降级返回原生404)
10. 思维导图
https://kdocs.cn/join/gpuxq6r?f=101\r\n邀请你加入共享群「老男孩教育Linux运维99期-孙克旭」一起进行文档协作