一份简单够用的 Nginx Location 配置讲解

简介: Location 是 Nginx 中一个非常核心的配置,这篇重点讲解一下 Location 的配置问题以及一些注意事项。

0.png


前言


Location 是 Nginx 中一个非常核心的配置,这篇重点讲解一下 Location 的配置问题以及一些注意事项。


语法


关于 Location,举个简单的配置例子:


http { 
  server {
      listen 80;
      server_name www.yayujs.com;
      location / {
        root /home/www/ts/;
        index index.html;
      }
  }
}
复制代码


大致的意思是,当你访问 www.yayujs.com80 端口的时候,返回 /home/www/ts/index.html 文件。


我们看下 Location 的具体语法:


location [ = | ~ | ~* | ^~ ] uri { ... }
复制代码


重点看方括号中的 [ = | ~ | ~* | ^~ ],其中 | 分隔的内容表示你可能会用到的语法,其中:


  • = 表示精确匹配,比如:


location = /test {
  return 200 "hello";
}
# /test ok
# /test/ not ok
# /test2 not ok
# /test/2 not ok
复制代码


  • ~ 表示区分大小写的正则匹配,比如:


location ~ ^/test$ {
  [ configuration ] 
}
# /test ok
# /Test not ok
# /test/ not ok
# /test2 not ok
复制代码


  • ~* 表示不区分大小写的正则匹配


location ~* ^/test$ {     
  [ configuration ] 
}
# /test ok
# /Test ok
# /test/ not ok
# /test2 not ok
复制代码


  • ^~ 表示 uri 以某个字符串开头


location ^~ /images/ {    
  [ configuration ] 
}
# /images/1.gif ok
复制代码


而当你不使用这些语法的时候,只写 uri 的时候:


/ 表示通用匹配:


location / {     
  [ configuration ] 
}
# /index.html ok
复制代码


location /test {
    [ configuration ] 
}
# /test ok
# /test2 ok
# /test/ ok
复制代码


匹配顺序


当存在多个 location 的时候,他们的匹配顺序引用 Nginx 官方文档就是:


A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.


If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.


Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.


翻译整理后就是:


location 的定义分为两种:


  • 前缀字符串(prefix string)
  • 正则表达式(regular expression),具体为前面带 ~*~ 修饰符的


而匹配 location 的顺序为:


  1. 检查使用前缀字符串的 locations,在使用前缀字符串的 locations 中选择最长匹配的,并将结果进行储存
  2. 如果符合带有 = 修饰符的 URI,则立刻停止匹配
  3. 如果符合带有  ^~ 修饰符的 URI,则也立刻停止匹配。
  4. 然后按照定义文件的顺序,检查正则表达式,匹配到就停止
  5. 当正则表达式匹配不到的时候,使用之前储存的前缀字符串


再总结一下就是:


在顺序上,前缀字符串顺序不重要,按照匹配长度来确定,正则表达式则按照定义顺序。


在优先级上,= 修饰符最高,^~ 次之,再者是正则,最后是前缀字符串匹配。


我们举几个简单的例子复习下:


server {
    location /doc {
        [ configuration A ] 
    }
    location /docu {
        [ configuration B ] 
    }
}
# 请求 /document 使用 configuration B
# 虽然 /doc 也能匹配到,但在顺序上,前缀字符串顺序不重要,按照匹配长度来确定
复制代码


server {
    location ~ ^/doc {
        [ configuration A ] 
    }
    location ~ ^/docu {
        [ configuration B ] 
    }
}
# 请求 /document 使用 configuration A
# 虽然 ~ ^/docu 也能匹配到,但正则表达式则按照定义顺序
复制代码


server {
    location ^~ /doc {
        [ configuration A ] 
    }
    location ~ ^/docu {
        [ configuration B ] 
    }
}
# 请求 /document 使用 configuration A
# 虽然 ~ ^/docu 也能匹配到,但 ^~ 的优先级更高
复制代码


server {
    location /document {
        [ configuration A ] 
    }
    location ~ ^/docu {
        [ configuration B ] 
    }
}
# 请求 /document 使用 configuration B
# 虽然 /document 也能匹配到,但正则的优先级更高
复制代码


root 与 alias 的区别


当我们这样设置 root 的时候:


location /i/ {
    root /data/w3;
}
复制代码


当请求 /i/top.gif/data/w3/i/top.gif 会被返回。


当我们这样设置 alias 的时候:


location /i/ {
    alias /data/w3/images/;
}
复制代码


当请求 /i/top.gif/data/w3/images/top.gif 会被返回。


乍一看两者很像,但细一看,就能看出两者的区别,root 是直接拼接 root + location 而 alias 是用 alias 替换 location,所以 root 中最后的路径里有 /i/,而 alias 中最后的路径里没有  /i/


所以如果你这样使用 allias 定义一个路径:


location /images/ {
    alias /data/w3/images/;
}
复制代码


其实使用 root 会更好:


location /images/ {
    root /data/w3;
}
复制代码


server 和 location 中的 root


server 和 location 中都可以使用 root,举个例子:


http { 
  server {
      listen 80;
      server_name www.yayujs.com;
      root /home/www/website/;
      location / {
        root /home/www/ts/;
        index index.html;
      }
  }
}
复制代码


如果两者都出现,是怎样的优先级呢?


简单的来说,就是就近原则,如果 location 中能匹配到,就是用 location 中的 root 配置,忽略 server 中的 root,当 location 中匹配不到的时候,则使用 server 中的 root 配置。


系列文章


博客搭建系列是我至今写的唯一一个偏实战的系列教程,讲解如何使用 VuePress 搭建博客,并部署到 GitHub、Gitee、个人服务器等平台。


  1. 一篇带你用 VuePress + GitHub Pages 搭建博客


  1. 一篇教你代码同步 GitHub 和 Gitee


  1. 还不会用 GitHub Actions ?看看这篇


  1. Gitee 如何自动部署 Pages?还是用 GitHub Actions!


  1. 一份前端够用的 Linux 命令


微信:「mqyqingfeng」,加我进冴羽唯一的读者群。


如果有错误或者不严谨的地方,请务必给予指正,十分感谢。如果喜欢或者 有所启发,欢迎 star,对作者也是一种鼓励。



目录
相关文章
|
5月前
|
编解码 应用服务中间件 Linux
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
centos配置nginx-rtmp实现ffmpeg转码rtsp为rtmp视频流
533 1
|
9月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
891 87
|
9月前
|
负载均衡 应用服务中间件 nginx
Nginx配置与命令
Nginx 是一款高性能的 HTTP 和反向代理服务器,其配置文件灵活且功能强大。本文介绍了 Nginx 配置的基础结构和常用指令,包括全局块、Events 块、HTTP 块及 Server 块的配置方法,以及静态资源服务、反向代理、负载均衡、HTTPS 和 URL 重写等功能实现。此外,还提供了常用的 Nginx 命令操作,如启动、停止、重载配置和日志管理等,帮助用户高效管理和优化服务器性能。
|
5月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
350 18
|
5月前
|
Ubuntu 安全 应用服务中间件
详细指南:配置Nginx服务器在Ubuntu平台上
以上步骤涵盖了基本流程:从软件包管理器获取 Ngnix, 设置系统服务, 调整UFW规则, 创建并激活服务器块(也称作虚拟主机), 并进行了初步优化与加固措施。这些操作都是建立在命令行界面上,并假设用户具有必要权限(通常是root用户)来执行这些命令。每个操作都有其特定原因:例如,设置开机启动确保了即使重启后也能自动运行 Ngnix;而编辑server block则定义了如何处理进入特定域名请求等等。
507 17
|
6月前
|
数据建模 应用服务中间件 PHP
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
本示例演示如何通过Docker挂载同一宿主目录至Nginx与PHP容器,实现PHP项目运行环境配置。需注意PHP容器中监听地址修改为0.0.0.0:9000,并调整Nginx配置中fastcgi_pass指向正确的IP与端口。同时确保Nginx容器中/var/www/html权限正确,以避免访问问题。
配置nginx容器和php容器协同工作成功,使用ip加端口的方式进行通信
|
7月前
|
应用服务中间件 网络安全 nginx
配置Nginx以支持Websocket连接的方法。
通过上述配置,Nginx将能够理解WebSocket协议的特殊要求,代理Websocket流量到合适的后端服务器。注意,Websocket并不是HTTP,尽管它最初是通过HTTP请求启动的连接升级,因此保证Nginx了解并能够妥善处理这种升级流程是关键。
1682 10
|
6月前
|
Ubuntu 应用服务中间件 Linux
在Ubuntu上配置Nginx实现开机自启功能
至此,Nginx应该已经被正确地设置为开机自启。在Ubuntu中利用 `systemd`对服务进行管理是一种高效的方式,为系统管理员提供了强大的服务管理能力,包括但不限于启动、停止、重启服务,以及配置服务的开机自启动。通过这些简洁的命令,即使是对Linux不太熟悉的用户也能轻松地进行配置。
318 0
|
缓存 应用服务中间件 网络安全
Nginx中配置HTTP2协议的方法
Nginx中配置HTTP2协议的方法
1090 7
|
8月前
|
安全 应用服务中间件 网络安全
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡
618 0
Nginx SSL/TLS协议栈中配置深度解析与实践指南-优雅草卓伊凡