[英]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.