【问题标题】:Nginx: Serving all content from the same directory on the server (even though the URL changes)Nginx:为服务器上同一目录中的所有内容提供服务(即使 URL 更改)
【发布时间】:2020-05-11 17:13:57
【问题描述】:

我构建了一个 React 应用程序,所有文件都从我的 Nginx 服务器上的同一物理目录提供。我使用 react-router-dom 浏览应用程序。当我第一次加载应用程序时,一切正常。

我将导航到 react-router-dom 知道的 URL(例如 http://example.com/blog/posts/),但是当我尝试刷新页面时,Nginx 以 404 Not Found 响应。我知道这是因为 Nginx 正在服务器上寻找与 /blog/posts 匹配的子文件夹。

我不知道如何告诉 Nginx 提供同一根文件夹中的所有内容不管浏览器中的路径。

所以不管走什么路……

/blog  
/blog/posts  
/blog/posts/1  
/contact
/summary/page/4   
....  

...所有文件都来自服务器上的同一个/etc/nginx/sites-available/my.domain.com/html/index.html 文件(这是应用程序的根页面)

当我刷新页面时,所有这些版本都不起作用:

location / {
     alias /etc/nginx/sites-available/my.domain.com/html;
     index index.html;
 }
location / {
     root /etc/nginx/sites-available/my.domain.com/html;
     index index.html;
 }
location ~ ^/ {
     root /etc/nginx/sites-available/my.domain.com/html;
     index index.html;
 }

【问题讨论】:

标签: reactjs nginx nginx-location


【解决方案1】:

在这里找到答案:

https://stackoverflow.com/a/43954597/1067109

location / {
  try_files $uri /index.html;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 2012-11-20
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多