nginx的重写对pushState的-URL的 [英] Rewriting nginx for pushState-URL's
问题描述
我试图让 nginx的
我的 pushState的
基于URI处理那些 Backbone.js的
在应用程序的Javascript管理我。
现在访问的URI的一个水平,比如。 example.com/users
效果很好,但不是两个级别的或更深的URI,如 example.com/users/all
,这是在骨干文档提到:
的例如,如果你有一个路径/文件/ 100,Web服务器
必须能够服务于该页面,如果浏览器访问这个URL
直接的
块引用>那么,是远离nginx的的重写选项熟悉,我仍然相信,我可以这样做
改写^ /index.html;
的一切重定向到我的index.html的
,但任何最终的静态文件丢失了(图像,JavaScript和放大器; CSS)。存储,我需要能够访问同一台服务器上所以,我应该怎么做,而不是如下图所示,当前的配置,以使这项工作?
服务器{
听80;
SERVER_NAME example.com; 位置 / {
根/var/www/example.com;
try_files $ URI /index.html;
}}
解决方案下面是我做我的应用程序。以'/'结尾的路线(除了根它的自我)将成为
index.html的
:位置〜^ / + / $ {
改写* /index.html持续。
}您也可以preFIX路线:
Backbone.history.start({pushState的:真正的根:/ preFIX /})
和则:
位置〜^ / preFIX / {
改写* /index.html持续。
}或定义为每个个案的规则。
I am trying to get
nginx
to work with mypushState
-based URI handling thatbackbone.js
manages for me in an Javascript app.Right now accessing URI's with one level, eg.
example.com/users
works well, but not two-level or deeper URI's, such asexample.com/users/all
, which is mentioned in the Backbone documentation:For example, if you have a route of /documents/100, your web server must be able to serve that page, if the browser visits that URL directly
So, being far from acquainted with nginx's rewrite options, I am still sure that I can do something like
rewrite ^ /index.html;
to redirect everything to myindex.html
, but loosing out on any eventual static files (images, javascript & css) stored on the same server which I need to be able to access.So what should I do instead with the below shown, current configuration, to make this work?
server { listen 80; server_name example.com; location / { root /var/www/example.com; try_files $uri /index.html; } }
解决方案Here is what i did to my application. Every route ending with a '/' (except the root it self) will serve
index.html
:location ~ ^/.+/$ { rewrite .* /index.html last; }
You can also prefix your route :
Backbone.history.start({pushState: true, root: "/prefix/"})
and then :
location ~ ^/prefix/ { rewrite .* /index.html last; }
Or define a rule for each case.
这篇关于nginx的重写对pushState的-URL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!