[英]How to configure Nginx to serve always one static file for all locations matching pattern?
我有一个 laravel 应用程序在 nginx 上提供服务,另一个应用程序只是通过反应创建一个 static 站点,该站点仅使用客户端路由 ZAF1A636669A3
到目前为止一切顺利,现在我想实现以下目标:
所有带有domain.com/admin/[whateverhere]模式的 url 都应提供以下 static html 文件
/var/www/html/admin_app/public/index.html
(反应 static 管理应用程序)而不是由
/var/www/html/users_app/public/index.php
应用程序)
这是我的 nginx 配置仅用于这部分,不起作用但返回 404:
location /admin/ {
root /var/www/html/admin_app/public;
index index.html;
try_files $uri index.html;
}
config的rest就是ssl和主域。
我不希望 laravel 应用程序处理 /admin/projects /admin/users 路由,但我希望所有这些到 go 到 index.htlm 文件,它是一个 ZA81259CEF8E959C624ZZD456 仅与客户端反应的应用程序。
通过检查 nginx 的外观,我看到它试图读取不存在的 html 文件。 不是我在 /var/www/html/admin_app/public/index.html 指定的那个
2020/12/01 15:30:31 [error] 6#6: *8 open() "/etc/nginx/htmlindex.html" failed (2: No such file or directory),
我不知道为什么 nginx 试图打开这个 /etc/nginx/htmlindex.html 文件......
有两种可能的解决方案...
首先,使用带有root
和try_files
指令的正则表达式location
:
location ~ ^/admin(/.*)$ {
root /var/www/html/admin_app/public;
try_files $1 /index.html =404;
}
正则表达式location
是按顺序计算的,因此配置文件中的位置很重要。 放置在server
块的开头附近以避免不必要的副作用。 有关详细信息,请参阅此文档。
请注意,永远不会达到=404
,它只是存在于/index.html
不是try_files
语句的最后一个参数。 有关详细信息,请参阅此文档。
或者,使用带有alias
指令的前缀location
:
location ^~ /admin/ {
alias /var/www/html/admin_app/public/;
if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}
location
值和alias
值都应该以/
结尾,或者都不以/
结尾。
由于这个长期问题, alias
和try_files
指令不能很好地协同工作,因此if
块。 请参阅有关在location
块内使用if
的注意事项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.