簡體   English   中英

如何在 Nginx 中為根 url 提供特定文件?

[英]How can I serve a particular file for root url in Nginx?

我需要從我的文件系統中為GET /請求提供文件。 我嘗試了以下方法:

location = / {
  index page2257160.html;
  root /var/www/site/;
}

其余請求被代理到后端:

location / {
  proxy_pass http://localhost:1234;
}

但是當我執行請求時,nginx 不是從文件系統提供文件,而是向后端詢問/page2257160.html ,后端返回 404,nginx 將此 404 發送回客戶端。

我怎樣才能解決這個問題?

index指令執行內部重定向,因此您需要第二個位置來匹配重寫的 URI。 例如:

root /var/www/site/;
location = / {
    index page2257160.html;
}
location = /page2257160.html {
}

有關詳細信息,請參閱 此文檔


您可以使用一個location塊和一個try_files指令來實現相同的目的。 例如:

location = / {
    root /var/www/site/;
    try_files /page2257160.html =404;
}

有關更多信息,請參閱此文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM