【nginx】root alias 区别,以及server root , location root 区别

最近在研究前后端分离站点配置在同一域名下,发现root,alias有区别,而且所有的root如果都放置在location下面访问无效的问题,才有此总结,本文只是作者自己的个人见解,非喜勿喷

root , alias 区别

  • root的配置时带匹配路径的
location /i/ {
    root /data/w3/;
}
### 如果访问 www.domain.com/i/test.png 最后返回的结果为 /data/w3/i/test.png ,root 配置最后的/要不要都行
  • alias的配置时不带匹配路径的,而且最后一定要加/,nginx可以处理多个////为一个/
location /i/ {
    alias /data/w3/;
}
### 如果访问 www.domain.com/i/test.png 最后返回的结果为 /data/w3/test.png ,root 配置最后的/一定是要的,否则返回结果会变成  /data/w3test.png ,root 而返回的404

server root , location root 区别

root 指的是请求的根目录,引用nginx官网的解释:

Sets the root directory for requests . A path to the file is constructed by merely adding a URI to the value of the root directive 翻译:设置请求的根目录,设置的文件路径要加上root后面匹配的URI
Note that the root directive is placed in the server context. Such root directive is used when the location block selected for serving a request does not include own root directive. 如果匹配的location里面没有自己的root指令,才用server里面的root指令

总结:location里面的root优先级高于server

问题:如果不设置server的root直接设置location root会在一些情况下找不到文件返回 no input file ,这个怎么解释呢?
答:解决的办法是设置 server 的root 然后再在location里面覆盖它

猜你喜欢

转载自www.cnblogs.com/china-flint/p/10313804.html
今日推荐