nginx--location

location介绍

匹配指定的请求uri(请求uri不包含查询字符串,如http://localhost:8000/test?age=18,请求url是/test)

语法:location   [ = | ~ | ~* | ^~ | @]   /uri/     { configuration }

匹配的优先级

匹配符 匹配规则 优先级
=    精确匹配    # 1
^~    以某个字符串开头   # 2
~    区分大小写的正则匹配    # 3
~*    不区分大小写的正则匹配   # 4
!~    区分大小写不匹配的正则   # 5
!~*    不区分大小写不匹配的正则   # 6
/    通用匹配,任何请求都会匹配到   # 7

配置文件

server {
    listen 80;
    server_name www.zouzou.com;

    # 优先级1,精确匹配,根路径
    location =/ {
        return 400;
    }

    # 优先级2,以某个字符串开头,以test开头的,优先匹配这里,区分大小写
    location ^~ /test{
       root /data/test/;
    }

    # 优先级3,区分大小写的正则匹配,匹配/media*****路径
    location ~ /media {
          alias /data/static/;
    }

    # 优先级4 ,不区分大小写的正则匹配,所有的****.jpg|gif|png 都走这里
    location ~* .*\.(jpg|gif|png|js|css)$ {
       root  /data/av/;
        }

    # 优先7,通用匹配
    location / {
        return 403;
    }
}

root和location

nginx指定文件路径有root和alias两种方法,区别在方法和作用域:

方法:root

语法 root 路径;

默认值 root html;

配置块 http{} server {} location{}

alias:
语法: alias 路径

配置块 location{}

root和alias区别在nginx如何解释location后面的url,这会使得两者分别以不同的方式讲请求映射到服务器文件上,root参数是root路径+location位置

# root实例1:

    location ^~ /test {
        root /data/test;   # 注意这里可有可无结尾的   /
    }
# 请求url是zouzou.com/test/index.html时
# web服务器会返回服务器上的/data/test/test/index.html

# root实例2: location ~* .*\.(jpg|gif|png|js|css)$ { root /data/test/; } # 请求url是zouzou.com/girl.gif时 # web服务器会返回服务器上的/data/test/girl.gif # alias实例: # alias参数是使用alias路径替换location路径,alias是一个目录的别名 # 注意alias必须有 "/" 结束!alias只能位于location块中 # 请求url是pythonav.cn/av/index.html时 # web服务器会返回服务器上的/data/static/index.html location ^~ /av { alias /data/static/; }

猜你喜欢

转载自www.cnblogs.com/zouzou-busy/p/11624521.html