nginx路径匹配
语法: location [=|~|~*|^~] /uri/ { ... }
- ~* 正则匹配不区分大小写
- ~ 正则匹配区分大小写
- ^~ 路径匹配,匹配后立即终止,不再尝试匹配正则
- = 完全匹配,匹配后立即终止,不再尝试其他任何匹配
优先级:
完全匹配 > 带^~的路径匹配 > 正则(多个正则时优先级按配置顺序) > 路径匹配
官方举例:
location = / {
# matches the query / only.
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
[ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
[ configuration D ]
}
Example requests:
- / -> configuration A
- /documents/document.html -> configuration B
- /images/1.gif -> configuration C
- /documents/1.jpg -> configuration D