翼度科技»论坛 云主机 LINUX 查看内容

Nginx Location 基本配置

12

主题

12

帖子

36

积分

新手上路

Rank: 1

积分
36
基本语法格式:
  1. Location block 的基本语法形式是:
  2.     location [=|~|~*|^~|@] pattern { ... }
  3. [=|~|~*|^~|@] 被称作 location modifier ,这会定义 Nginx 如何去匹配其后的 pattern ,以及该 pattern 的最基本的属性(简单字符串或正则表达式)
复制代码
location 的匹配符

1.等于匹配符:=

等于匹配符就是等号,特点可以概括为两点:精确匹配,优先级最高,匹配成功后则停止向下搜索不支持正则表达式
  1. server {
  2.     server_name website.com;
  3.     location = /abcd {
  4.     […]
  5.     }
  6. }
  7. 匹配情况:
  8.     http://website.com/abcd        # 正好完全匹配
  9.     http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
  10.     http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
  11.     http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),Nginx 不认为这种情况是完全匹配
  12.     http://website.com/abcde    # 不匹配,因为不是完全匹配
复制代码
  
2.空匹配符

空匹配符的特点是:匹配以指定模式开始的 URI,区分大小写,字符串匹配不支持正则表达式
  1. # 可以不写 location modifier ,Nginx 仍然能去匹配 pattern 。这种情况下,匹配那些以指定的 patern 开头的 URI,注意这里的 URI 只能是普通字符串,不能使用正则表达式。
  2. server {
  3.     server_name website.com;
  4.     location /abcd {
  5.     […]
  6.     }
  7. }
  8. 匹配情况:
  9.     http://website.com/abcd        # 正好完全匹配
  10.     http://website.com/ABCD        # 如果运行 Nginx server 的系统本身对大小写不敏感,比如 Windows ,那么也匹配
  11.     http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
  12.     http://website.com/abcd/    # 末尾存在反斜杠(trailing slash)也属于匹配范围内
  13.     http://website.com/abcde    # 仍然匹配,因为 URI 是以 pattern 开头的
复制代码
  
3.正则匹配符:~ 和 ~*

正则匹配符是可以使用正则表达式的匹配符。不过这里要强调的是,一般来说~是指:区分大小写的正则匹配但是对于一些对大小写不敏感的操作系统,这两者没有区别。另外一个就是^~,其表示以指定模式开始的正则匹配。
  1. # 这个 location modifier 对大小写敏感,且 pattern 须是正则表达式
  2. server {
  3.     server_name website.com;
  4.     location ~ ^/abcd$ {
  5.     […]
  6.     }
  7. }
  8. 匹配情况:
  9.     http://website.com/abcd        # 完全匹配
  10.     http://website.com/ABCD        # 不匹配,~ 对大小写是敏感的
  11.     http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
  12.     http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
  13.     http://website.com/abcde    # 不匹配正则表达式 ^/abcd$
  14.    
  15. # 注意:对于一些对大小写不敏感的系统,比如 Windows ,~ 和 ~* 都是不起作用的,这主要是操作系统的原因。
复制代码
   
~* 表示:不区分大小写的正则匹配
  1. server {
  2.     server_name website.com;
  3.     location ~* ^/abcd$ {
  4.     […]
  5.     }
  6. }
  7. 匹配情况:
  8.     http://website.com/abcd        # 完全匹配
  9.     http://website.com/ABCD        # 匹配,这就是它不区分大小写的特性
  10.     http://website.com/abcd?param1m2    # 忽略查询串参数(query string arguments),这里就是 /abcd 后面的 ?param1m2
  11.     http://website.com/abcd/    # 不匹配,因为末尾存在反斜杠(trailing slash),并不匹配正则表达式 ^/abcd$
  12.     http://website.com/abcde    # 不匹配正则表达式 ^/abcd$
复制代码
  
4. ^~

匹配情况类似 2. (空字符) 的情况,以指定匹配模式开头的 URI 被匹配,不同的是,一旦匹配成功,那么 Nginx 就停止去寻找其他的 Location 块进行匹配了(与 Location 匹配顺序有关) 5. 内部访问符:@

用于定义一个 Location 块,且该块不能被外部 Client 所访问,只能被 Nginx 内部配置指令所访问,比如 try_files or error_page。一般用于错误页面等。 匹配符优先级

Location匹配规则 语法规则: 【= | ^~ | ~ | ~* | / | /uri 】
  1. location = /uri        = 表示精确匹配,只有完全匹配上才能生效,若找到,停止搜索;
  2. location ^~ /uri       ^~开头表示对URL路径进行前缀匹配,并且在正则匹配之前,若找到,停止搜索;
  3. location ~ pattern     ~开头表示区分大小写的正则匹配,按配置文件顺序匹配;
  4. location ~* pattern    ~*开头表示不区分大小写的正则匹配,按配置文件顺序匹配;
  5. location !~和!~*       分别为区分大小写不匹配及不区分大小写不匹配 的正则
  6. location /uri          不带任何修饰符,表示前缀匹配,在正则匹配之后;
  7. location /              通用匹配,任何未匹配到其他location的请求都会匹配到,相当于default;
复制代码
   
多个location配置的情况匹配顺序为
  1. 首先精确匹配 = ;
  2. 其次前缀匹配 ^~;
  3. 其次是按照配置文件中的正则匹配;
  4. 然后匹配不带任何修饰符的前缀匹配;
  5. 最后交给/通用匹配
复制代码
 
案例 
  1. location  = / {
  2.   # 精确匹配 / ,主机名后面不能带任何字符串
  3.   [ configuration A ] 
  4. }
  5. location  / {
  6.   # 因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求
  7.   # 但是正则和最长字符串会优先匹配
  8.   [ configuration B ] 
  9. }
  10. location /documents/ {
  11.   # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  12.   # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  13.   [ configuration C ] 
  14. }
  15. location ~ /documents/Abc {
  16.   # 匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索
  17.   # 只有后面的正则表达式没有匹配到时,这一条才会采用这一条
  18.   [ configuration CC ] 
  19. }
  20. location ^~ /images/ {
  21.   # 匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条。
  22.   [ configuration D ] 
  23. }
  24. location ~* \.(gif|jpg|jpeg)$ {
  25.   # 匹配所有以 gif,jpg或jpeg 结尾的请求
  26.   # 然而,所有请求 /images/ 下的图片会被 config D 处理,因为 ^~ 到达不了这一条正则
  27.   [ configuration E ] 
  28. }
  29. location /images/ {
  30.   # 字符匹配到 /images/,继续往下,会发现 ^~ 存在
  31.   [ configuration F ] 
  32. }
  33. location /images/abc {
  34.   # 最长字符匹配到 /images/abc,继续往下,会发现 ^~ 存在
  35.   # F与G的放置顺序是没有关系的
  36.   [ configuration G ] 
  37. }
  38. location ~ /images/abc/ {
  39.   # 只有去掉 config D 才有效:先最长匹配 config G 开头的地址,继续往下搜索,匹配到这一条正则,采用
  40.     [ configuration H ] 
  41. }
  42. location ~* /js/.*/\.js
复制代码
 
 

来源:https://www.cnblogs.com/qianyuliang/p/17463260.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具