Nginx处理用户请求流程⭐⭐⭐⭐⭐
注意清理浏览器缓存和重启服务
ip访问问题⭐⭐⭐⭐⭐
访问方式 |
|
域名访问 |
与虚拟主机中的server_name进行匹配 |
ip访问 |
没有配置默认的虚拟主机:按照子配置文件字母排序(使用第一个) |
配置默认的虚拟主机:在需要配置为默认的虚拟主机文件中配置( listen 80 default_server;) |
Nginx处理用户请求流程⭐⭐⭐⭐⭐
- DNS解析:域名---->IP地址
- 连接80端口:tcp三次握手与网站的80端口建立连接
- http请求报文:请求方式、URI、HOST
- GET / HTTP/1.1
- Host: xiaoniao.master.cn
- User-Agent: curl/8.9.1
- Nginx处理
- http请求:http区域处理
- 不同的server{}区域处理
- 端口
- 域名:用户请求的域名与虚拟主机中的server_name进行匹配
- 匹配成功就让对应的虚拟主机(server{})处理
- 根据虚拟主机中的root、index、location规则处理查找文件
- 将查找到的文件返回给用户
- http响应报文
- 状态码:200 OK
- server信息
- 其他信息
- 文件内容
- 客户端收到文件内容,浏览器进行解析并展示
Nginx核心功能与模块⭐⭐⭐⭐⭐
虚拟主机⭐⭐⭐⭐⭐
允许在单一的物理服务器上托管多个网站或应用的技术,使得多个域名可以共享一个ip
概述与分类
虚拟主机的分类 |
说明 |
应用场景 |
基于域名的虚拟主机 |
不同域名访问不同站点 |
同一台服务器上托管多个网站,每个网站都有不同的域名 |
基于端口的虚拟主机 |
不同端口访问不同站点 |
同一台服务器上托管多个应用,每个应用使用不同的端口 |
基于ip的虚拟主机 |
不同ip访问不同的站点 |
同一台服务器上托管多个网站,每个网站都有不同的ip地址 |
基于域名的虚拟主机⭐⭐⭐⭐⭐
- 本地测试域名的技巧:curl -v -H "HOST: wuziqi.master.cn" http://10.0.0.7
- -H 修改请求头里面的内容
# 案例:创建wuziqi.master.cn网站,站点目录/app/code/wuziqi-master/
server{
listen 80;
server_name wuziqi.master.cn;
root /app/code/wuziqi-master;
location /
{ index index.html; }
}
# 不创建站点目录
curl -v -H "HOST: wuziqi.master.cn" http://10.0.0.7
# 提示404 Not Found
# 创建站点目录,但是没有首页文件
curl -v -H "HOST: wuziqi.mast.cn" http://10.0.0.7
# 没有指定访问首页,提示403 Forbidden
curl -v -H "HOST: wuziqi.mast.cn" http://10.0.0.7/index.html
# 指定访问首页,提示404 Not Found
基于端口的虚拟主机(了解)
# 修改wuziqi.master.cn虚拟主机监听端口为81
server {listen 81;}
# 访问
# 命令行访问
crul -H "HOST: wuziqi.master.cn" http:/10.0.0.7:81/index
# 浏览器访问
http://10.0.0.7:81/index.html
基于IP虚拟主机(了解)
# 案例:wuziqi.master.cn网站,端口为8888,且只能通过172.16.1.7内网访问
# 修改wuziqi.master.cn
server{ listen 172.16.1.7:8888; }
只有能发送请求到ip地址为172.16.1.7的客户端可以访问Nginx服务器
# 测试(在172.16.1.0/24内网节点上测试)
curl -v -H "HOST: wuziqi.master.cn" 172.16.1.7:8888
Nginx日志 ⭐⭐⭐
- 给每个虚拟主机指定独立的错误日志
- 给每个虚拟主机指定独立的访问日志
概述
日志 |
功能 |
默认位置 |
用途 |
错误日志 |
记录错误的信息 |
/var/log/nginx/error.log |
问题排查,服务器监控 |
访问日志 |
记录请求的详细信息 |
/var/log/nginx/access.log |
流量分析,用户行为监测 |
错误日志⭐⭐⭐
# 指定错误日志的位置和错误级别
# error_log指令
格式:error_log 文件名 错误日志级别;
# 错误日志级别
debug>info>notice>warn>error>crit>alert>emerg
# error_log指令存放位置
main、http、mail、stream、server、location
# 案例:给每个虚拟主机指定独立的错误日志
[root@web01 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/wuziqi.master.cn.conf
server{
server_name wuziqi.master.cn;
root /app/code/wuziqi-master;
error_log /var/log/nginx/wuziqi.master.cn-error.log notice;
location /
{
index index.html;
}
}
# 执行nginx -t检查语法命令会自动创建对应的日志文件(目录不存在会报错)
# 将nginx.conf里面的error_log注释
访问日志⭐⭐⭐
Nginx访问日志格式(nginx内置变量) |
说明 |
$remote_addr |
客户端的ip地址 |
$remote_user |
客户端用户名 |
$time_local |
请求时间 |
$request |
请求报文的起始行 |
$status |
http状态码 |
$body_bytes_sent |
发送给客户端的字节数 |
$http_referer |
请求来源 |
$http_user_agent |
客户端的代理字符串 |
nginx内置变量:https://nginx.org/en/docs/varindex.html

access_log指定访问日志 |
access_log 日志位置 格式 |
access_log指令存放位置:http,server,location,if in location,init_except |
# 给每个虚拟主机指定自己独立的访问日志
# 需要将nginx.conf里面的access_log注释
[root@web01 ~]# cat /etc/nginx/conf.d/xiaoniao.master.cn.conf
server{
server_name xiaoniao.master.cn;
root /app/code/xiaoniao-master;
error_log /var/log/nginx/xiaoniao.master.cn-error.log;
access_log /var/log/nginx/xiaoniao.master.cn-access.log main;
location / {
index index.html;
}
}
Location规则⭐⭐⭐⭐⭐
URI与URL⭐
# URI:统一资源标识符
# URL:统一资源定位符
https://gitee.com/yuanxiaojiang
URI:/yuanxiaojiang # 域名后面的内容
URL:https://gitee.com/yuanxiaojiang # 网址
https://www.cnblogs.com/yuanxiaojiang
URI:https://www.cnblogs.com/yuanxiaojiang
URL:/yuanxiaojiang
案例01:搭建大型直播购物网站(allow deny)⭐⭐⭐
- 域名:www.buy.cn
- 站点目录:/app/code/buy/
- 后台管理页面:
- /app/code/buy/admin/index.html
- 要求后台只能内网访问:172.16.1.0/24网段
[root@web01 ~]# tree /app/code/buy/
/app/code/buy/
├── admin
│ └── index.html
└── index.html
[root@web01 /etc/nginx/conf.d]# cat www.buy.cn.conf
server{
listen 80;
server_name www.buy.cn.conf;
root /app/code/buy/;
location / {
index index.html;
}
location /admin/ {
allow 172.16.1.0/24; # 允许172.16.1.0/24网段访问
deny all; # 拒绝所有
}
}

案例02:搭建小鸟飞飞网站(expires)
- 网站中html、js、css结尾的文件缓存一天
- 网站中图片文件缓存一小时
[root@web01 /etc/nginx/conf.d]# cat xiaoniao.master.cn.conf
server{
server_name xiaoniao.master.cn;
root /app/code/xiaoniao-master;
error_log /var/log/nginx/xiaoniao.master.cn-error.log;
access_log /var/log/nginx/xiaoniao.master.cn-access.log main;
location / {
index index.html;
}
location ~* \.(html|js|css)$ {
# expires max;
expires 1d;
}
location ~* \.(jpg|jpeg|png|gif|bmp)$ {
expires 1h;
}
}

location规则小结⭐⭐⭐⭐⭐
- location [modifier] uri {...}
- modifier:可选的修饰符,用于改变匹配规则(=、~、~*、^~)
- uri:要匹配的URI或路径
location规则 |
说明 |
location / {} |
表示匹配所有以“/”开头的请求。默认规则,如果请求没有被其他更具体的location块匹配到,都会由这个location /{}块来处理 |
location /image/ {} |
前缀匹配:匹配以/image/开头的URI |
location ~ \.(jpg|jpeg)$ {} |
区分大小写的正则匹配 |
location ~* \.(jpg|jpeg)$ {} |
不区分大小写的正则匹配 |
location ^~ /image/ {} |
前缀匹配优先,不支持正则:当IRI以/image/开头时,将不再检查后续的正则表达式规则 |
location = /about |
精确匹配:仅匹配/about,不会匹配/about/或/about.html等 |
location @名字 {} |
命令的location一般用于跳转 |
优先级 |
= ^~ ~ ~* /image/ / |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律