PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Nginx服务器中的location怎么配置

WBOY
WBOY 转载
2023-05-14 19:16:12 2144浏览

语法
location [=|~|~*|^~] /uri/ {...}

规则
= : 表示精确的uri匹配(有兴趣的同学可以看一下url和uri的区别)
~: 表示区分大小写的正则匹配
~*:表示不区分大小写的正则匹配
!~ && !~*:表示区分大小写不匹配的正则和不区分大小写的不匹配的正则
/:通用匹配,任何请求都会匹配到

location匹配目标
location匹配测试只使用请求uri的部分,而不使用参数部分。(原因:参数的写法太多,无法精确匹配)

location匹配顺序
多个location配置的前提下,location的匹配顺序(未验证,嘿嘿,google上搜的)
1.首先匹配=
2.其次匹配^~
3.再其次按照配置文件的顺序进行正则匹配、
4.最后是交给/进行通用匹配
注意:
当有匹配成功时,立刻停止匹配,按照当前匹配规则处理请求

演示实例

nginx 配置文件,自下到上分为三种层次分明的结构:
| http block the protocol level
| server block the server level
v location block the requested uri

nginx 允许用户定义 location block ,并指定一个匹配模式(pattern)匹配特定的 uri。除了简单的字符串(比如文件系统路径),还允许使用更为复杂的匹配模式(pattern)。
location block 的基本语法形式是:

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

[=|~|~*|^~|@] 被称作 location modifier ,这会定义 nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)。

关于 location modifier

1. =

这会完全匹配指定的 pattern ,且这里的 pattern 被限制成简单的字符串,也就是说这里不能使用正则表达式。
example:

server {
  server_name jb51.net;
  location = /abcd {
  […]
  }
}

匹配情况:
http://jb51.net/abcd # 正好完全匹配
http://jb51.net/abcd # 如果运行 nginx server 的系统本身对大小写不敏感,比如 windows ,那么也匹配
http://jb51.net/abcd?param1?m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
http://jb51.net/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),nginx 不认为这种情况是完全匹配
http://jb51.net/abcde # 不匹配,因为不是完全匹配

2. (none)
可以不写 location modifier ,nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 uri,注意这里的 uri 只能是普通字符串,不能使用正则表达式。
example:

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

匹配情况:
http://jb51.net/abcd # 正好完全匹配
http://jb51.net/abcd # 如果运行 nginx server 的系统本身对大小写不敏感,比如 windows ,那么也匹配
http://jb51.net/abcd?param1?m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
http://jb51.net/abcd/ # 末尾存在反斜杠(trailing slash)也属于匹配范围内
http://jb51.net/abcde # 仍然匹配,因为 uri 是以 pattern 开头的

3. ~
这个 location modifier 对大小写敏感,且 pattern 须是正则表达式

example:
server {
  server_name jb51.net;
  location ~ ^/abcd$ {
  […]
  }
}

匹配情况:
http://jb51.net/abcd # 完全匹配
http://jb51.net/abcd # 不匹配,~ 对大小写是敏感的
http://jb51.net/abcd?param1?m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
http://jb51.net/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
http://jb51.net/abcde # 不匹配正则表达式 ^/abcd$
注意:对于一些对大小写不敏感的系统,比如 windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。

4. ~*
与 ~ 类似,但这个 location modifier 不区分大小写,pattern 须是正则表达式
example:

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

匹配情况:
http://jb51.net/abcd # 完全匹配
http://jb51.net/abcd # 匹配,这就是它不区分大小写的特性
http://jb51.net/abcd?param1?m2 # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1?m2
http://jb51.net/abcd/ # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
http://jb51.net/abcde # 不匹配正则表达式 ^/abcd$

5. ^~
匹配情况类似 2. (none) 的情况,以指定匹配模式开头的 uri 被匹配,不同的是,一旦匹配成功,那么 nginx 就停止去寻找其他的 location 块进行匹配了(与 location 匹配顺序有关)

6. @
用于定义一个 location 块,且该块不能被外部 client 所访问,只能被 nginx 内部配置指令所访问,比如 try_files or error_page

演示实例

Nginx服务器中的location怎么配置

产生的效果如下:
访问根目录/,匹配到location /
访问除hello.php之外的其它php程序,匹配到location ~ \.php$,并且用php5-fpm去运行
访问hello.php,匹配到location = /hello.php,访问被重定向到好联系官网

以上就是Nginx服务器中的location怎么配置的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:亿速云,如有侵犯,请联系admin@php.cn删除