之前一直没有注意过Nginx中的这个alias参数
当要访问的url直接对应本机的某个path时
都是使用root来做处理的
有时候,某个url是从前端的反向代理一直代理过来
url的前面某个路径是虚拟的path,这样就需要先rewrite下

location ^~ /html/1/{
root /opt/test/html/;
rewrite "^/html/1/(.*)$" /$1;
break;
}


就是先把前面的/html/1给去掉,然后在本机的/opt/test/html/下寻找

今天才知道,其实alias就是干这个活的,直接这么用就ok了

location ^~ /html/1/{
alias /opt/test/html/;



看nginx的官方文档吧

alias
syntax: alias file-path|directory-path;
default: no
context: location
This directive assigns a path to be used for the indicated location. Note that it may look similar to the root directive, but the document root doesn't change, just the file system path used for the request.

For example:

location  /i/ {
  alias  /spool/w3/images/;
}
The request "/i/top.gif" will return the file "/spool/w3/images/top.gif".

It is possible to use variables in the replacement path.
The alias directive cannot be used inside a regex-specified location. If you need to do this you must use a combination of rewrite and root.



注意最后一段,alias不能适用于正则匹配
如果要在正则里使用,则只能用前面所说的rewrite和root了...