nginx - location 语法

本文详细介绍了Nginx中location配置的作用与语法,包括精准匹配、一般匹配及正则匹配等不同方式,并通过实例说明了这些配置是如何影响网站资源定位与处理流程的。

location 语法
location 有”定位”的意思, 根据Uri来进行不同的定位.
在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上.
比如, 碰到.php, 如何调用PHP解释器? –这时就需要location
location 的语法

location [=|~|~*|^~] patt {
}

中括号可以不写任何参数,此时称为一般匹配
也可以写参数
因此,大类型可以分为3种
location = patt {} [精准匹配]
location patt{} [一般匹配]
location ~ patt{} [正则匹配]

如何发挥作用?:
首先看有没有精准匹配,如果有,则停止匹配过程.

location = patt {
    config A
}

如果 $uri == patt,匹配成功,使用configA

location = / {
   root   /var/www/html/;
   index  index.htm index.html;
}

location / {
  root   /usr/local/nginx/html;
  index  index.html index.htm;
}

如果访问  http://xxx.com/

定位流程是 

1: 精准匹配中 ”/” ,得到index页为  index.htm
2: 再次访问 /index.htm , 此次内部转跳uri已经是”/index.htm” ,
根目录为/usr/local/nginx/html
3: 最终结果,访问了 /usr/local/nginx/html/index.htm

再来看,正则也来参与.
location / {
     root   /usr/local/nginx/html;
     index  index.html index.htm;
 }

location ~ image {
    root /var/www/image;
    index index.html;
}

如果我们访问 http://xx.com/image/logo.png
此时, “/” 与”/image/logo.png” 匹配
同时,”image”正则 与”image/logo.png”也能匹配,谁发挥作用?
正则表达式的成果将会使用.

图片真正会访问 /var/www/image/logo.png

location / {
  root   /usr/local/nginx/html;
  index  index.html index.htm;
}

location /foo {
  root /var/www/html;
  index index.html;
}

我们访问 http://xxx.com/foo
对于uri “/foo”, 两个location的patt,都能匹配他们
即 ‘/’能从左前缀匹配 ‘/foo’, ‘/foo’也能左前缀匹配’/foo’,
此时, 真正访问 /var/www/html/index.html
原因:’/foo’匹配的更长,因此使用之.;

这里写图片描述

你执行了 `nginx -t` 命令,输出如下: ``` nginx: the configuration file E:\nginx-1.28.0/conf/nginx.conf syntax is ok nginx: configuration file E:\nginx-1.28.0/conf/nginx.conf test is successful ``` ✅ **这表示:你的 Nginx 配置文件语法正确,并且通过了测试!** --- ## ✅ 详细解释 ### 📌 `nginx -t` 是什么? 这是 Nginx 提供的一个**配置文件检测命令**,用于: - 检查 `nginx.conf` 是否存在语法错误 - 验证配置是否可加载(但不启动服务) ### 🧩 输出含义逐行解析: ```bash nginx: the configuration file E:\nginx-1.28.0/conf/nginx.conf syntax is ok ``` > ✔️ 配置文件语法没问题,没有拼写错误、括号不匹配等问题。 ```bash nginx: configuration file E:\nginx-1.28.0/conf/nginx.conf test is successful ``` > ✔️ 整体配置验证成功,Nginx 可以基于此配置正常启动。 --- ## ✅ 下一步建议 你现在可以安全地启动 Nginx: ```bash E:\nginx-1.28.0> start nginx ``` 或使用完整路径方式启动: ```bash E:\nginx-1.28.0> nginx.exe ``` > ⚠️ 注意:如果端口被占用(如 80 端口),可能会启动失败。可通过修改 `conf/nginx.conf` 中的 `listen 80;` 改为其他端口(如 `listen 8080;`)解决。 --- ## 🔍 补充常用命令(Windows 环境) | 命令 | 作用 | |------|------| | `nginx -t` | 测试配置文件语法 | | `nginx` 或 `start nginx` | 启动 Nginx | | `nginx -s reload` | 重新加载配置(热重启) | | `nginx -s quit` | 安全退出(优雅关闭) | | `nginx -s stop` | 快速停止(强制终止) | | `taskkill /IM nginx.exe /F` | 强制结束所有 nginx 进程(当无法退出时) | --- ## 💡 示例:修改监听端口避免冲突 如果你发现启动后访问不了,可能是 80 端口被占用(常见于 IIS、Apache、VMware 等)。 编辑 `conf/nginx.conf`: ```nginx server { listen 8080; # 修改为 8080 server_name localhost; location / { root html; index index.html index.htm; } } ``` 保存后再次测试并重启: ```bash E:\nginx-1.28.0> nginx -t E:\nginx-1.28.0> nginx -s reload ``` 然后访问:[http://localhost:8080](http://localhost:8080) --- ## ✅ 如何确认 Nginx 已运行? 打开任务管理器 → 查看是否有多个 `nginx.exe` 进程(通常一个 master process + 一个 worker process)。 或者在命令行输入: ```bash tasklist /FI "IMAGENAME eq nginx.exe" ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值