`

nginx location说明

 
阅读更多
原文
引用

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location

Sets configuration depending on a request URI.

The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.

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.

location blocks can be nested, with some exceptions mentioned below.

For case-insensitive operating systems such as Mac OS X and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.

Regular expressions can contain captures (0.7.40) that can later be used in other directives.

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.

    In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” modifiers, the search also terminated and regular expressions were not checked.

Let’s illustrate the above by an example:

    location = / {
        [ configuration A ]
    }

    location / {
        [ configuration B ]
    }

    location /documents/ {
        [ configuration C ]
    }

    location ^~ /images/ {
        [ configuration D ]
    }

    location ~* \.(gif|jpg|jpeg)$ {
        [ configuration E ]
    }

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

    location /user/ {
        proxy_pass http://user.example.com;
    }

    location = /user {
        proxy_pass http://login.example.com;
    }



uri匹配规则
1 先decode uri
2 解析 "."和".."相对路径
3、多个"/"会被合并为一个"/"
4 支持字符串前缀(以xxxx字符开头的uri)匹配和正则匹配
5 以“~*”和“~”开头为正则匹配,“~*”正则大小写不敏感,“~”大小写精确匹配
6 如果匹配到的最长的字符串前缀匹配前有“^~”,则不再匹配正则表达式

一次匹配过程
先搜索字符串前缀匹配,最长的字符串前缀优先匹配,作为临时结果,继续匹配正则,匹配到第一个正则表达式后,匹配结束,使用这个正则表达式的配置,若未搜到正匹配的正则,则使用临时匹配上的字符串匹配

对示例的讲解
引用

Let’s illustrate the above by an example:

    精确匹配
    location = / {
        [ configuration A ]
    }
    字符串前缀匹配
    location / {
        [ configuration B ]
    }
    字符串前缀匹配 最长字符串匹配规则
    location /documents/ {
        [ configuration C ]
    }
    字符串前缀匹配 如果匹配到这个配置,则放弃正则匹配
    location ^~ /images/ {
        [ configuration D ]
    }
    正则匹配
    location ~* \.(gif|jpg|jpeg)$ {
        [ configuration E ]
    }

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.


“/”精确匹配了 配置A

“/index.html”字符串前缀 匹配了配置B,以"/"开头的url

“/documents/document.html”字符串前缀 匹配了配置C,以"/documents/"开头的匹配,
同时也匹配了 配置规则B,以"/"开头的URL,字符串前缀匹配以最长匹配优先,所以最终
匹配结果为配置C,


“/images/1.gif” 匹配配置D和E,但是D配置在前,因此结果为D

“/documents/1.jpg” 匹配配置C和E,但C是字符串前缀匹配,会继续进行正则匹配E,匹配到E后以E为匹配结果,匹配结束


分享到:
评论

相关推荐

    NGINX location 在配置中的优先级.docx

    NGINX location 配置中的优先级详解 NGINX 中的 location directive 是一个非常重要的配置指令,它可以根据不同的 URL 模式来匹配不同的请求路径。但是,location 的配置顺序并不是固定的,而是根据 Location ...

    nginx的各项详细配置-超多注释

    本篇文章将详细介绍Nginx的各项配置,并结合提供的"nginx.conf"配置文件和"nginx.txt"说明文件,深入解析Nginx的配置语法和应用场景。** ### 1. Nginx基本结构 Nginx的配置文件主要由多个块组成,包括全局块、...

    nginx搭建配置详细说明

    3.4. nginx重要指令之location 4. nginx中的rewrite 4.1. 什么是rewrite 4.2. rewrite的命令的作用域和优先级 4.3. if指令 4.3.1. if指令的语法 4.3.2. if指令中使用的逻辑运算符 4.3.3. If指令中可以使用的...

    nginx文档说明

    ### Nginx 实现 Tomcat 集群负载均衡配置详解 #### 一、引言 随着互联网技术的发展,单一服务器往往难以满足高并发场景的需求。为了提高系统的可用性和响应能力,采用负载均衡技术成为了一种常见的解决方案。Nginx ...

    nginx location中uri的截取的实现方法

    说明: location 中的 root 和 alias root 指令只是将搜索的根设置为 root 设定的目录,即不会截断 uri,而是使用原始 uri 跳转该目录下查找文件 aias 指令则会截断匹配的 uri,然后使用 alias 设定的路径加上...

    Nginx 反向代理 location 语法规则说明

    Nginx 反向代理 Location 语法规则详解 Nginx 的 Location 语法规则是 Nginx 反向代理中最重要的配置部分之一。Location 规则用于匹配客户端请求的 URI,并将其路由到相应的服务器或应用程序。下面我们详细解释 ...

    Nginx完整配置说明

    Nginx完整配置说明 Nginx是当前最流行的Web服务器软件之一,常用于搭建Web服务器、反向代理服务器、负载均衡器等。下面是Nginx的完整配置说明,涵盖基本配置、反向代理、FastCGI等方面的知识点。 一、基本配置 在...

    Nginx配置文件说明.

    Nginx的配置文件通常位于/etc/nginx/nginx.conf或/usr/local/nginx/conf/nginx.conf,根据不同的系统和安装路径可能会有所差异。 在Nginx的配置文件中,主要有以下几个部分: 1. **全局块**:这部分设置影响Nginx...

    详解Nginx Location配置

    Location指令允许你定义一系列规则,以便当客户端请求到达时,Nginx能够选择最适合的处理策略。 首先,理解Nginx配置文件的基本结构至关重要。配置文件主要由Server和Location两个区块构成。Server区块定义了服务器...

    nginx.config_nginx_

    **Nginx基础配置与参数说明** Nginx是一款高性能的HTTP和反向代理服务器,以其轻量级、高并发处理能力以及优秀的稳定性而被广泛应用于互联网服务中。本文将详细解析`nginx.config`文件中的核心配置选项和参数,帮助...

    nginx使用说明1

    location / { proxy_pass http://backend; # 转发到 upstream proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } ...

    Nginx使用说明与常见问题解析.docx

    ### Nginx 使用说明与常见问题解析 #### 一、Nginx 安装与配置 **1.1 安装 Nginx** Nginx 的安装方式因操作系统而异。以下是不同系统上的安装步骤: - **Ubuntu/Debian:** ```bash sudo apt update sudo apt ...

    Nginx配置文件说明

    以下是对标题和描述中提及的Nginx配置文件部分知识点的详细说明: 1. **基本配置** - `user www www`: 这行指定Nginx运行时使用的用户和组,通常为非root用户以提高安全性。 - `worker_processes 1`: 定义了Nginx...

    Nginx安装使用说明.docx

    **Nginx安装使用说明** **1. 编写目的** 本文档的目的是为用户提供详细的Nginx安装和使用步骤,旨在帮助用户快速理解和掌握如何在Linux和Windows操作系统上部署和配置Nginx web服务器。 **2. 背景** Nginx是一款高...

    nginx简单配置说明(开发人员了解)

    ### Nginx简单配置说明(开发人员了解) #### 概述 本文档旨在为开发人员提供一份简明的Nginx配置指南。通过本指南,读者可以了解到如何在本地环境中安装并配置Nginx,使其能够顺利运行。Nginx是一款高性能的HTTP...

    ubuntu+nginx安装配置应用说明

    在本文中,我们将深入探讨如何在Ubuntu操作系统上安装和配置Nginx服务器,这是一个流行的开源Web服务器,以其高性能和稳定性而闻名。Nginx广泛用于处理静态内容、反向代理和负载均衡等任务。 首先,确保你的Ubuntu...

    nginx1.8附安装手册

    在浏览器中输入服务器的 IP 地址,如果看到 Nginx 的欢迎页面,说明安装成功。 ### 六、配置 Nginx 服务器块 Nginx 使用服务器块来管理多个网站。创建一个简单的服务器块示例: ```bash sudo vi /usr/local/nginx...

    nginx 安装及配置文档

    **Nginx简介** Nginx 是一款高性能的 Web 和反向代理服务器,以其轻量级、高并发处理能力和稳定性而著称。它被广泛应用于静态文件服务、动态内容代理、负载均衡等领域,是现代互联网架构中的关键组件之一。 **安装...

Global site tag (gtag.js) - Google Analytics