简体   繁体   English

如何配置 Nginx 以始终为所有位置匹配模式提供一个 static 文件?

[英]How to configure Nginx to serve always one static file for all locations matching pattern?

I have one laravel application served over nginx, and another application just with react that creates a static site that uses only client side routing react-router.我有一个 laravel 应用程序在 nginx 上提供服务,另一个应用程序只是通过反应创建一个 static 站点,该站点仅使用客户端路由 ZAF1A636669A3

So far so good, now I want to achieve the following :到目前为止一切顺利,现在我想实现以下目标

all urls with pattern domain.com/admin/[whateverhere] should be served the following static html file所有带有domain.com/admin/[whateverhere]模式的 url 都应提供以下 static html 文件

/var/www/html/admin_app/public/index.html (react static admin app) instead of being handled by /var/www/html/admin_app/public/index.html (反应 static 管理应用程序)而不是由

/var/www/html/users_app/public/index.php (the laravel app) /var/www/html/users_app/public/index.php应用程序)

This is my nginx configuration only for this part, that does not work but returns 404:这是我的 nginx 配置仅用于这部分,不起作用但返回 404:

   location /admin/ {
      root /var/www/html/admin_app/public;
      index index.html;
      try_files $uri index.html;
    }

The rest of config is just ssl, and main domain. config的rest就是ssl和主域。

I don't want the laravel app to handle /admin/projects /admin/users routes, but I want all of these to go to just that index.htlm file which is a static react app, with client side router only.我不希望 laravel 应用程序处理 /admin/projects /admin/users 路由,但我希望所有这些到 go 到 index.htlm 文件,它是一个 ZA81259CEF8E959C624ZZD456 仅与客户端反应的应用程序。

By checking nginx looks, I see that it tries to read non existing html file.通过检查 nginx 的外观,我看到它试图读取不存在的 html 文件。 not the one I specify at /var/www/html/admin_app/public/index.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),

I have no idea why nginx tries to open this /etc/nginx/htmlindex.html file...我不知道为什么 nginx 试图打开这个 /etc/nginx/htmlindex.html 文件......

There are two potential solutions...有两种可能的解决方案...

First, using a regular expression location with a root and try_files directive:首先,使用带有roottry_files指令的正则表达式location

location ~ ^/admin(/.*)$ {
    root /var/www/html/admin_app/public;
    try_files $1 /index.html =404;
}

The regular expression location is evaluated in order, so the placement within the configuration file is significant.正则表达式location是按顺序计算的,因此配置文件中的位置很重要。 Place near the beginning of the server block to avoid unwanted side effects.放置在server块的开头附近以避免不必要的副作用。 See this document for details.有关详细信息,请参阅此文档

Note that the =404 is never reached, it just exists to that /index.html is not the last parameter of the try_files statement.请注意,永远不会达到=404 ,它只是存在于/index.html不是try_files语句的最后一个参数。 See this document for details.有关详细信息,请参阅此文档


Alternatively, using a prefix location with an alias directive:或者,使用带有alias指令的前缀location

location ^~ /admin/ {
    alias /var/www/html/admin_app/public/;
    if (!-e $request_filename) { rewrite ^ /admin/index.html last; }
}

Both the location value and alias value should end with a / , or neither end with a / . location值和alias值都应该以/结尾,或者都不以/结尾。

The alias and try_files directives do not work well together due to this long term issue , hence the if block.由于这个长期问题aliastry_files指令不能很好地协同工作,因此if块。 See this caution on the use of if within a location block.请参阅有关在location块内使用if注意事项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM