水下功夫做透,水上才能顺风顺水。

nignx配置解释

一. 配置结构

 

 二. 全局块配置

 

三. events块配置

 

四. http块配置

location路径匹配

1. "="精确匹配

 

location = /abc/ {
  .....
 }
        
# 只匹配http://abc.com/abc
#http://abc.com/abc [匹配成功]
#http://abc.com/abc/index [匹配失败]

 

2. "~" 区分大小写的正则匹配

location ~ /abc/ {
  .....
}
#http://abc.com/Abc/ [匹配失败]
#http://abc.com/abc/ [匹配成功]

3."~*" 忽略大小写的正则匹配

location ~* /Abc/ {
  .....
}
# 则会忽略 uri 部分的大小写
#http://abc.com/Abc/ [匹配成功]
#http://abc.com/abc/ [匹配成功]

4. "^~" 前缀匹配

location ^~ /index/ {
  .....
}
#以 /index/ 开头的请求,都会匹配上。前缀匹配上以后,不再进行后面的匹配
#http://abc.com/index/index.page  [匹配成功]
#http://abc.com/error/error.page [匹配失败]

5. 不加任何规则时,默认是大小写敏感的前缀匹配

location /index/ {
  ......
}
#http://abc.com/index  [匹配成功]
#http://abc.com/index/index.page  [匹配成功]
#http://abc.com/test/index  [匹配失败]
#http://abc.com/Index  [匹配失败]
# 匹配到所有uri

6. 内部跳转

location /index/ {
  error_page 404 @index_error;
}
location @index_error {
  .....
}
#以 /index/ 开头的请求,如果链接的状态为 404。则会匹配到 @index_error 这条规则上。

7. uri/

(1)如果 URI 结构是  的形式

  尾部有没有 / 都不会造成重定向。因为浏览器在发起请求的时候,默认加上了 / 

(2)如果 URI 的结构是  

  尾部如果缺少 / 将导致重定向。因为根据约定,URL 尾部的 / 表示目录,没有 / 表示文件。

    /some-dir/ 时,服务器会自动去该目录下找对应的默认文件;

    /some-dir 时,服务器会先去找 some-dir 文件,找不到的话会将 some-dir 当成目录,重定向到 /some-dir/ ,去该目录下找默认文件

 匹配优先级:= 优于 ^~ 优于 ~ | ~* 优于 最长前缀匹配 优于 /

location内部配置

 

  location / {
        root /var/www/;
        index index.htm index.html;
    }

 

当用户请求 / 地址时,Nginx 就会自动在 root(根目录)配置指令指定的目录下依次寻找 index.htm 和index.html 这两个文件,找到则进行内部跳转。如果都找不到,则放弃处理权给content阶段的下一个模块。

alias & root 的区别,root是根目录匹配路径会附在后面,alias是别名是匹配路径的替代。
location /img/ { alias /var/www/image/; } #访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
}
#访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。

 

posted @ 2024-02-18 11:39  北方寒士  阅读(5)  评论(0编辑  收藏  举报