nginx之location、rewrite配置

 location:

location ^~ /images/ {
root html;
}

^~ :以 以某个路径开头

访问路径:

http://localhost/images/sec/index.html

http://localhost/images/index.html

 rewrite:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

rewrite  地址重写 相当于servlet 的foward,地址栏地址不会变化

location /index.html{
root html/flag;
}
location /aa.html{
rewrite ^/ /index.html break;

#rewrite ^/ /index.html last;

#rewrite ^/ /index.html;
root html/static/;
}

rewrite last: 使地址重新进行location配置;而break 只进行当前匹配;

没有参数时,如果有两个rewrite时,则显示第二个rewrie内容匹配到的内容;

如上配置对于: /images/ttt/test.png 会重写到/mic?file=test.png, 于是匹配到 location /mic ; 通过try_files获取存在的文件进行返回。最后由于文件不存在所以直接

返回404错误

原文地址:https://www.cnblogs.com/newlangwen/p/10408487.html