为 pushState-URL 重写 nginx [英] Rewriting nginx for pushState-URL's

查看:18
本文介绍了为 pushState-URL 重写 nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 nginx 与基于 pushState 的 URI 处理一起工作,backbone.js 在 Javascript 应用程序中为我管理.

I am trying to get nginx to work with my pushState-based URI handling that backbone.js manages for me in an Javascript app.

现在访问一个级别的 URI,例如.example.com/users 运行良好,但不是两级或更深的 URI,例如 example.com/users/all,它在 Backbone 文档:

Right now accessing URI's with one level, eg. example.com/users works well, but not two-level or deeper URI's, such as example.com/users/all, which is mentioned in the Backbone documentation:

例如,如果你有一个/documents/100 的路由,你的 web 服务器必须能够提供该页面,如果浏览器访问该 URL直接

因此,由于对 nginx 的重写选项还很不熟悉,我仍然确信我可以执行诸如 rewrite ^/index.html; 之类的操作来将所有内容重定向到我的 index.html,但会丢失存储在我需要能够访问的同一台服务器上的任何最终静态文件(图像、javascript 和 css).

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 my index.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;
    }

}

推荐答案

这是我对我的应用程序所做的.每条以/"结尾的路由(除了它本身的根)都将服务于 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/"})

然后:

  location ~ ^/prefix/ {
    rewrite .* /index.html last;
  }

或者为每个案例定义一个规则.

Or define a rule for each case.

这篇关于为 pushState-URL 重写 nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆