如何解决Nginx忽略位置内部的root指令
我正在将Nginx作为Docker容器运行,并在容器内的/react-frontend
处安装了一个卷。
我为React应用程序配置了一个简单的配置,但是其中的root指令似乎被忽略了。我不明白为什么。
位于conf.d
目录中的单个配置文件:
server {
listen 80;
root /ive/got/a/feeling;
location /react {
root /react-frontend/build;
rewrite /react(.*) $1 break;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
失败:
2020/08/18 04:32:18 [error] 181#181: *38823 open() "/ive/got/a/feeling/index.html" failed (2: No such file or directory)...
"GET /react/home HTTP/1.1" 404 /ive/got/a/feeling/index.html
但是,只需将根指令移至该位置之外:
server {
listen 80;
root /react-frontend/build;
location /react {
rewrite /react(.*) $1 break;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
有效:
"GET /react/home HTTP/1.1" 200 /react-frontend/build/index.html
我希望第一个配置能够正常工作。我以为某个位置内的root指令会覆盖服务器根,但这似乎没有发生。 谁能解释一下?
非常感谢!
解决方法
root
外部的location {}
指令被视为父目录,因此,当您尝试访问/react/*
时,服务器将解析请求并尝试找到react
目录在/ive/got/a/feeling
您要做的是使用/
创建另一个根目录,并删除位于root
之外的location {}
指令
server {
listen 80;
location / {
root /ive/got/a/feeling;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /react {
root /react-frontend/build;
rewrite /react(.*) $1 break;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。