GVKun编程网logo

nginx(2):location 命令(nginx location/)

23

本文将带您了解关于nginx的新内容,同时我们还将为您解释2:location命令的相关知识,另外,我们还将为您提供关于NginxLocation、nginxlocation优先级、NginxLoca

本文将带您了解关于nginx的新内容,同时我们还将为您解释2:location 命令的相关知识,另外,我们还将为您提供关于Nginx Location、nginx location 优先级、Nginx Location 模块、nginx location 规则的实用信息。

本文目录一览:

nginx(2):location 命令(nginx location/)

nginx(2):location 命令(nginx location/)

location 匹配命令

~      # 波浪线表示执行一个正则匹配,区分大小写
~*    #表示执行一个正则匹配,不区分大小写
^~    #^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
=      # 进行普通字符精确匹配
@     #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

location 匹配的优先级 (与 location 在配置文件中的顺序无关)
= 精确匹配会第一个被处理。如果发现精确匹配,nginx 停止搜索其他匹配。
普通字符匹配,正则表达式规则和长的块规则将被优先和查询匹配,也就是说如果该项匹配还需去看有没有正则表达式匹配和更长的匹配。
^~ 则只匹配该规则,nginx 停止搜索其他匹配,否则 nginx 会继续处理其他 location 指令。
最后匹配理带有 "~" 和 "~*" 的指令,如果找到相应的匹配,则 nginx 停止搜索其他匹配;当没有正则表达式或者没有正则表达式被匹配的情况下,那么匹配程度最高的逐字匹配指令会被使用。

location 优先级官方文档

  1. Directives with the = prefix that match the query exactly. If found, searching stops.

  2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.

  3. Regular expressions, in order of definition in the configuration file.

  4. If #3 yielded a match, that result is used. Else the match from #2 is used.

  1. = 前缀的指令严格匹配这个查询。如果找到,停止搜索。

  2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用 ^〜前缀,搜索停止。

  3. 正则表达式,在配置文件中定义的顺序。

  4. 如果第 3 条规则产生匹配的话,结果被使用。否则,如同从第 2 条规则被使用。

 

例如

location  = / {
  # 只匹配"/".
  [ configuration A ] 
}
location  / {
  # 匹配任何请求,因为所有请求都是以"/"开始
  # 但是更长字符匹配或者正则表达式匹配会优先匹配
  [ configuration B ] 
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location
  [ configuration C ] 
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配以 gif, jpg, or jpeg结尾的请求. 
  # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.   
  [ configuration D ] 
}

请求 URI 例子:

  • /-> 符合 configuration A

  • /documents/document.html -> 符合 configuration B

  • /images/1.gif -> 符合 configuration C

  • /documents/1.jpg -> 符合 configuration D

@location 例子
error_page 404 = @fetch ;

location @fetch(
proxy_pass http://fetch;
)


Nginx Location

Nginx Location

location 是 nginx 配置中一个指令,用于访问 URL 配置,而在这个 location 中所配置的每个指令将启动不同的模块去完成相应工作。

默认 nginx.conf 中至少有一个 location /, 即表示客户端浏览器请求的 URL 为域名 +/, 常见方式如下:

=                         字面精确匹配;
^~                         最大前缀匹配;
/                         不带任何前缀:最大前缀匹配;
~                         大小写相关的正则匹配;
~*                        大小写无关的正则匹配;
@                        location内部重定向的变量。

Location = 精确匹配会被第一个处理,如果发现精确匹配,Nginx 则停止搜索其他任何 Location 的匹配

^~ 为最大前缀匹配,如果匹配到该规则,Nginx 则停止搜索其他任何 location 的匹配,否则 Nginx 会继续处理其他 location 指令

Location 规则优先级如果:

(location =)>(location 完整路径)>(location ^~ 路径)>(location ~|~*) >(location 部分起始路劲)>(location /)

如下为 Nginx Location 规则案例

location  = / {
  [ configuration  L1 ] 
  #只会匹配/,优先级比Location /低。
}
location  = /index.html {
  [ configuration  L2 ]  
#只会匹配/index.html,优先级最高。
}
location  / {
  [ configuration L3 ] 
  #匹配任何请求,因为所有请求都是以"/"开始;
  #但是更长字符匹配或者正则表达式匹配会优先匹配,优先级最低。
}
location = /images/ {
  [ configuration L4 ] 
  #匹配任何以/images/开始的请求,并停止匹配其它location;
}
location ~* \.(html|txt|gif|jpg|jpeg)$ { 
  [ configuration L5] 
  # 匹配以html、txt、gif、jpg、jpeg结尾的URL文件请求; 
  # 但是所有/images/目录的请求将由 [Configuration L4]处理。
}
浏览器发起HTTP Request URI案例与Location规则案例匹配如下:
    / ->匹配configuration L3;
    /index.html匹配configuration L2;
    /images/匹配configuration L4;
    /images/logo.png匹配configuration L4;
    /img/test.jpg匹配configuration L5。

常见添加如下:

location /
{
    root /var/www/html/
    expires      60d;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
    root /var/www/html/
    expires      60d;      
}
location ~ .*\.(jsp|php|cgi|do)$
{
    root /var/www/html/
    proxy_pass http://linux_web;
    proxy_http_version 1.1
    proxy_set_header Connection ""
    proxy_set_header Host  $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
}
location =/newindex.html
{
    root /var/www/newwww/
    expires      60d;
}

 

nginx location 优先级

nginx location 优先级

location 顺序/优先级:
     location = > location 完整路径 > location ^~ 路径 > location ~,~* 正则顺序 > location 部分起始路径 > /

location  = / {
  # 精确匹配 / ,主机名后面不能带任何字符串
  [ configuration A ]
}
location  / {
  # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  # 但是正则和最长字符串会优先匹配
  [ configuration B ]
}
location /documents/ {
  # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  [ configuration C ]
}
location ~ /documents/Abc {
  # 匹配任何以 /documents/Abc 开头的地址,匹配符合以后,还要继续往下搜索
  # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  [ configuration CC ]
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
  [ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配所有以 gif,jpg或jpeg 结尾的请求
  # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
  [ configuration E ]
}
location /images/ {
  # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
  [ configuration F ]
}
location /images/abc {
  # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
  # F与G的放置顺序是没有关系的
  [ configuration G ]
}
location ~ /images/abc/ {
  # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
    [ configuration H ]
}
location ~* /js/.*/\.js {
  # 不区分大小写匹配
  [ configuration I ]
}

= :开头表示精确匹配; 如: A 中只匹配根目录结尾的请求,后面不能带任何字符串;
^~ :开头表示uri以某个常规字符串开头,不是正则匹配;
~ :开头表示区分大小写的正则匹配;
~* :开头表示不区分大小写的正则匹配;
/ :通用匹配, 如果没有其它匹配,任何请求都会匹配到;

Nginx Location 模块

Nginx Location 模块

相关知识点:
  URI: 统一资源标识符,是一个用于标识某一互联网资源名称的字符串,该种标识允许用户对任何的资源通过特定的协议进行交互操作。
  URL: 统一资源定位符,由三部分组成

(1)http:// 协议

(2)存有该资源的主机 IP 地址

(3)主机资源的具体地址,如目录和文件名。
   

web 上可用的每种资源(HTML 文档、图像、视频片段、程序等)都由一个通用资源定位符(URL)进行定位。

location 块

  location 是 Nginx 配置中的一个指令,用于和访问的 URL 匹配,而在这个 location 中所匹配的每个指令将会启动不同的模块去完成相应的工作。

= 请求字符串与 URI 精确匹配,匹配成功,就停止搜索,立即执行。常用来匹配某一特定文件。
^~ 最大前缀匹配,处理字符串与 URI 标识匹配度最高的 location 块,不再使用 location 块中的正则 URI 与请求字符串进行匹配。
/ 最大前缀匹配,不带任何前缀。
~ 与大小写相关的正则匹配。
~* 与大小写无关的正则匹配。

  (location=)> (location 完整路径)>(location^~ 路径)>(location~ | location~* 正则顺序)>(location 部分起始路径)>(location /)

.*\.(gif)$ 描述

.* : 任何字符(. 任意 1 个字符,* 表示匹配多次)
\. : 转义.gif 前面的点号。
(gif) : 匹配 gif 字符,需要以 gif 结尾的字符才能匹配
$ : 必须以 gif 结尾。

root : 服务器接收到请求以后查找资源的根目录路径。
index : 匹配发布目录的默认的网站后缀名称。

更改 location 的 URI

alias 指令可以改变 location 接收到的 URI 的请求路径,语法结构如下:
alias path; #path 即为修改后的根路径。

例:

location ~ /data/.\(html)$
{
alias /locationtest/other/$1; #把访问目录 /data/ 改为 /locationtest/other/, 请求会访问 /locationtest/other/ 目录下.html 结尾的文件,而不再访问 /data/ 目录下的文件。
}

生产环境实际使用建议:

1. 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理。

2. 第一个必选规则:
location = / {
proxy_pass http://tomcat:8080/index.php
}

3. 第二个必选规则是处理静态文件请求,这是 Nginx 作为 http 服务器的强项。

4. 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用。
location ^~ /static/ {
root /webroot/static/ ;
}

location ~* \.(gif| | jpg | jpeg | png | css | js | ico )$ {
root /webroot/res/;
}

5. 第三个规则就是通用规则,用来转发静态请求到后端应用服务器。

6. 非静态文件请求就默认是动态请求,自己根据实际把握。
location / {
proxy_pass http://tomcat:8080/
}

 

nginx location 规则

nginx location 规则

To summarize, the order in which directives are checked is as follows:

1,Directives with the "=" prefix that match the query exactly. If found, searching stops.

2,All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.

3,Regular expressions, in the order they are defined in the configuration file.

4,If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

个人总结:

1,普通匹配的表达式不能重复,正则匹配的表达式不能重复(不同的修饰符可以重复,比如~和~*)

2,正则匹配:要有~(大小写敏感)获取~*(大小写不敏感) 普通匹配:^~ 或 = 或空

3,最大前缀匹配,uri 要尽量匹配更多的前缀

4,普通匹配没有编辑的顺序,正则有

5,正则 location 让位于 = 和 ^~,覆盖其他的普通 location

今天关于nginx2:location 命令的介绍到此结束,谢谢您的阅读,有关Nginx Location、nginx location 优先级、Nginx Location 模块、nginx location 规则等更多相关知识的信息可以在本站进行查询。

本文标签: