关于服务器配置的主干和推送状态

Backbone and pushstate with regards to server confuguration

本文关键字:状态 服务器配置      更新时间:2023-09-26

我想扩展我关于这个问题的问题。我能够让 pushstate 在我的骨干应用程序中正常工作。因此,在访问我的应用程序和导航链接时,我被重定向到localhost:8000/page而不是localhost:8000/#page。现在,重新加载本地主机:8000/页时,我的问题变得明显。显然,我的服务器会抛出错误,因为它找不到指定的页面。与 localhost:8000/#page 相反,它将调用正确的路由。现在假设我的应用程序在以下 localhost:8000/index.html。然后我是否必须请求将所有路由重定向到 localhost:8000/index.html。什么被认为是正确的方法?我发现这个要点是基于主干和推送状态的节点.js服务器配置。快速查看,似乎所有请求都只是重定向到应用程序文件(index.html)。这是对的吗?IIS运气好吗?

基本上,你需要在Apache或Nginx中重写一些规则。

Apache Vhost:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /
   RewriteRule ^index'.html$ - [L]
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . /index.html [L]
</IfModule>

嘟嘟:

 rewrite ^(.+)$ /index.html last;

对于实际的 API 调用,我在子域(如 api.site.com)上提供它们。为此,您可能需要启用 CORS。

我在这个博客条目中更深入地讨论了它:http://readystate4.com/2012/05/17/nginx-and-apache-rewrite-to-support-html5-pushstate/