推荐阅读
syntax: location [ ~ | ~* | ^~ | = ] /uri/ { ... }
无前缀
literal string
匹配~
和 ~*
~
: 大小写敏感~*
: 大小写不敏感^~
~
, 匹配成功则立即终止匹配=
exact query
location /i/ {
alias /path/to/images/;
}
# 剩余 location 直接拼接到 alias
# request /i/top.gif
# return /path/to/images/top.gif
alias
指令不能用在指定正则的 location
中;
指定正则的 location
中需要使用 rewrite
+ root
;
location /i/ {
root /path/to/images;
}
# 整个 location 直接拼接到 root
# request /i/top.gif
# return /path/to/images/i/top.gif
rewrite
位于 server
下时, 可以使用 last
标记来中止 rewrite
;
server {
# ...
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
return 403;
# ...
}
rewrite
位于 location
下时, 需要把 last
换成 break
;
location /download/ {
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
return 403;
}
replacement
包含新的 arguments
时, 需要在结尾添加 ?
, 因为原 arguments
会直接拼接到 replacement
后面
rewrite ^/users/(.*)$ /show?user=$1? last;
location / {
try_files index.html index.htm @whatever_fallback;
}
location @whatever_fallback {
root /var/www/error;
index index.html;
}
具名location
(named location
)@fallback location
;$args
(等同于 $query_string
)
请求参数
$request_filename
基于
root / alias
和request URI
的当前file path
$request_uri
包括
arguments
的完整初始uri
(complete initial URI
)
$uri
当前
uri
不同于initial uri
, 因为可能经过了内部重定向
/images/%20/test
, then you must use /images/ /test
to determine the location.如非特别声明,本站作品均为原创,遵循【自由转载-保持署名-非商用-非衍生 创意共享 3.0 许可证】。
对于转载作品,如需二次转载,请遵循原作许可。