Nginx - HTTP Configuration, the Location Block

时间:2022-08-29 15:33:36

Nginx offers you the possibility to fine-tune your configuration down to three levels — at the protocollevel (http block), the server
level (server block), and the requested URI level (location block). Let us now detail the latter.

Location Modifier

Nginx allows you to define location blocks by specifying a pattern that will be matched against the requested document URI.

server {
  server_name website.com;
  location /admin/ {
    # The configuration you place here only applies to
    # http://website.com/admin/
  }
}

Instead of a simple folder name, you can indeed insert complex patterns. The syntax of the location block is:

location [=|~|~*|^~|@] pattern { ... }

The first optional argument is a symbol called location modifier that will define the way Nginx matches the specified pattern and also defines the very nature of the pattern (simple string or regular expression). The following paragraphs detail the different modifiers and their behavior.

The = modifier

The requested document URI must match the specified pattern exactly. The pattern here is limited to a simple literal string; you cannot use a regular expression:

server {
  server_name website.com;
  location = /abcd {
    […]
  }
}

The configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Applies to http://website.com/ABCD (it is case-sensitive if your operating system uses a case-sensitive filesystem)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Does not apply to http://website.com/abcd/ (trailing slash)
  • Does not apply to http://website.com/abcde (extra characters after the specified pattern)

No modifier

The requested document URI must begin with the specified pattern. You may not use regular expressions:

server {
  server_name website.com;
  location /abcd {
    […]
  }
}

The configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Applies to http://website.com/ABCD (it is case-sensitive if your operating system uses a case-sensitive filesystem)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Applies to http://website.com/abcd/ (trailing slash)
  • Applies to http://website.com/abcde (extra characters after the specified pattern)

The ~ modifier

The requested URI must be a case-sensitive match to the specified regular expression:

server {
  server_name website.com;
  location ~ ^/abcd$ {
    […]
  }
}

The ^/abcd$ regular expression used in this example specifies that the pattern must begin (^) with /, be followed by abc, and finish ($) with d. Consequently, the configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Does not apply to http://website.com/ABCD (case-sensitive)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Does not apply to http://website.com/abcd/ (trailing slash) due to the specified regular expression
  • Does not apply to http://website.com/abcde (extra characters) due to the specified regular expression

With operating systems such as Microsoft Windows, ~ and ~* are both case-insensitive, as the OS uses a case-insensitive filesystem.

The ~* modifier

The requested URI must be a case-insensitive match to the specified regular expression:

server {
  server_name website.com;
  location ~* ^/abcd$ {
    […]
  }
}

The regular expression used in the example is similar to the previous one. Consequently, the configuration in the location block:

  • Applies to http://website.com/abcd (exact match)
  • Applies to http://website.com/ABCD (case-insensitive)
  • Applies to http://website.com/abcd?param1&param2 (regardless of query string arguments)
  • Does not apply to http://website.com/abcd/ (trailing slash) due to the specified regular expression
  • Does not apply to http://website.com/abcde (extra characters) due to the specified regular expression

The ^~ modifier

Similar to the no-symbol behavior, the location URI must begin with the specified pattern. The difference is that if the pattern is matched, Nginx stops searching for other patterns (read the section below about search order and priority).

The @ modifier

Defines a named location block. These blocks cannot be accessed by the client, but only by internal requests generated by other directives, such as try_files or error_page.

Search Order and Priority

Since it's possible to define multiple location blocks with different patterns, you need to understand that when Nginx receives a request, it searches for the location block that best matches the requested URI:

server {
  server_name website.com;
  location /files/ {
    # applies to any request starting with "/files/"
    # for example /files/doc.txt, /files/, /files/temp/
}
location = /files/ {
    # applies to the exact request to "/files/"
    # and as such does not apply to /files/doc.txt
    # but only /files/
  }
}

When a client visits http://website.com/files/doc.txt, the first location block applies. However, when they visit http://website.com/files/, the second block applies (even though the first one matches) because it has priority over the first one (it is an exact match).

The order you established in the configuration file (placing the /files/ block before the = /files/ block) is irrelevant. Nginx will search for matching patterns in a specific order:

  1. location blocks with the = modifier: If the specified string exactly matches the requested URI, Nginx retains the location block.
  2. location blocks with no modifier: If the specified string exactly matches the requested URI, Nginx retains the location block.
  3. location blocks with the ^~ modifier: If the specified string matches the beginning of the requested URI, Nginx retains the location block.
  4. location blocks with ~ or ~* modifier: If the regular expression matches the requested URI, Nginx retains the location block.
  5. location blocks with no modifier: If the specified string matches the beginning of the requested URI, Nginx retains the location block.

Case 1:

server {
  server_name website.com;
  location /doc {
    […] # requests beginning with "/doc"
  }
  location ~* ^/document$ {
    […] # requests exactly matching "/document"
  }
}

You might wonder: when a client requests http://website.com/document, which of these two location blocks applies? Indeed, both blocks match this request. Again, the answer does not lie in the order in which the blocks appear in the configuration files. In this case, the second location block will apply as the ~* modifier has priority over the other.

Case 2:

server {
  server_name website.com;
  location /document {
    […] # requests beginning with "/document"
  }
  location ~* ^/document$ {
    […] # requests exactly matching "/document"
  }
}

The question remains the same — what happens when a client sends a request to download http://website.com/document? There is a trick here. The string specified in the first block now exactly matches the requested URI. As a result, Nginx prefers it over the regular expression.

Case 3:

server {
  server_name website.com;
  location ^~ /doc {
    […] # requests beginning with "/doc"
  }
  location ~* ^/document$ {
    […] # requests exactly matching "/document"
  }
}

This last case makes use of the ^~ modifier. Which block applies when a client visits http://website.com/document? The answer is the first block. The reason being that ^~ has priority over ~*. As a result, any request with a URI beginning with /doc will be affected to the first block, even if the request URI matches the regular expression defined in the second block.

随机推荐

  1. HDU 5025 Saving Tang Monk --BFS

    题意:给一个地图,孙悟空(K)救唐僧(T),地图中'S'表示蛇,第一次到这要杀死蛇(蛇最多5条),多花费一分钟,'1'~'m'表示m个钥匙(m<=9),孙悟空要依次拿到这m个钥匙,然后才能去救唐 ...

  2. linux RWT

    http://www.cnblogs.com/qlwy/archive/2011/06/26/2121919.html#undefined

  3. jersey client上传下载文件

    jersey client上传文件demo File file = new File("/tmp/test.jpg"); System.out.println(file.exist ...

  4. Android底层音频声道耳机插头和开关壳体的发展

    Android潜在的发展耳机插头连接到音频频道切换的例子 因为使用的是耳机 麦克分离式的耳机,所以要分别上报事件.在Android系统层耳机插孔的检測是基于/sys/class/switch/h2w/ ...

  5. 关于Cookie的知识的总结

    Cookie的类型 会话cookie和持久cookie 会话cookie是一种临时cookie,它记录了用户访问站点时的设置和偏好,当用户退出浏览器时,会话cookie就会被删除. 持久cookie的 ...

  6. eclipse修改内存大小

    一般安装完eclipse之后,在安装目录下你应该可以看到有一个 eclipse.ini 文件,对了,就是在这个文件里面修改,我打开同事机器上这个文件,里面的内容是: -vmargs-Dosgi.spl ...

  7. aircrack-ng 工具集学习

    一.aircrack-ng简介 aircrack-ng是Aircrack项目的一个分支.是一个与802.11标准的无线网络分析有关的安全软件,主要功能有:网络侦测,数据包嗅探,WEP和WPA/WPA2 ...

  8. pickle &amp&semi; cPickle ValueError&colon; unsupported pickle protocol&colon; 3

    pickle and cPickle pickle和cPickle是python对象的转储文件,保存的是python对象 他们分别是python2和python3的对应部分,建议引入的时候采用以下方法 ...

  9. 如何写一个简单的HTTP服务器(重做版)

    最近几天用C++重新写了之前的HTTP服务器,对以前的代码进行改进.新的HTTP服务器采用Reactor模式,有多个线程并且每个线程有一个EventLoop,主程序将任务分发到每个线程,其中采用的是轮 ...

  10. 网络编程——TCP协议和通信

    第1章 TCP通信 TCP通信同UDP通信一样,都能实现两台计算机之间的通信,通信的两端都需要创建socket对象. 区别在于,UDP中只有发送端和接收端,不区分客户端与服务器端,计算机之间可以任意地 ...