关于javascript:一份简单够用的-Nginx-Location-配置讲解

77次阅读

共计 3718 个字符,预计需要花费 10 分钟才能阅读完成。

前言

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 搭建博客
  2. 一篇教你代码同步 GitHub 和 Gitee
  3. 还不会用 GitHub Actions?看看这篇
  4. Gitee 如何主动部署 Pages?还是用 GitHub Actions!
  5. 一份前端够用的 Linux 命令

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

如果有谬误或者不谨严的中央,请务必给予斧正,非常感激。如果喜爱或者 有所启发,欢送 star,对作者也是一种激励。

正文完
 0