重写 nginx for pushState-URL



我正在尝试让nginx使用backbone.js在Javascript应用程序中为我管理的基于pushState的URI处理。

目前,使用一个级别访问URI(例如example.com/users)效果良好,但不使用两级或更深层次的URI(例如主干文档中提到的example.com/users/all

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

因此,我还远远不熟悉nginx的重写选项,我仍然相信我可以做一些类似rewrite ^ /index.html;的事情来将所有内容重定向到我的index.html,但会失去存储在同一服务器上的任何最终静态文件(图像、javascript&css),我需要能够访问这些文件。

那么,我应该如何使用下面显示的当前配置来实现这一点呢?

server {
    listen   80;
    server_name  example.com;
    location / {
        root   /var/www/example.com;
        try_files $uri /index.html;
    }
}

我最终选择了这个解决方案:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+..+$ {
        try_files $uri =404;
    }
    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri /index.html;
    }
}

这样,如果找不到文件,至少我仍然会得到正确的404错误。

以下是我对应用程序所做的操作。每个以"/"结尾的路由(除了自身的根路由)都将服务于index.html:

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

您也可以在路线前加上前缀:

Backbone.history.start({pushState: true, root: "/prefix/"})

然后:

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

或者为每种情况定义一条规则。

我是这样管理的:

#set root and index
root /var/www/conferences/video/;
index  index.html;
#route all requests that don't serve a file through index.html
location / {
   if (!-e $request_filename){
      rewrite ^(.*)$ /index.html break;
   }
}

使用客户端应用程序路径:

/
/foo
/foo/bar
/foo/bar/baz
/foo/bar/baz/123
/tacos
/tacos/123

使用:

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    gzip_static on;
    location / {
      try_files $uri $uri/ /index.html;
    }
    # Attempt to load static files, if not found route to @rootfiles
    location ~ (.+).(html|json|txt|js|css|jpg|jpeg|gif|png|svg|ico|eot|otf|woff|woff2|ttf)$ {
      try_files $uri @rootfiles;
    }
    # Check for app route "directories" in the request uri and strip "directories"
    # from request, loading paths relative to root.
    location @rootfiles {
      rewrite ^/(?:foo/bar/baz|foo/bar|foo|tacos)/(.*) /$1 redirect;
    }
}

虽然@Adam Waite的答案适用于根和根级别的路径,但在位置上下文中使用if被认为是一种反模式,通常在转换Apache风格的指令时看到。请参阅:http://wiki.nginx.org/IfIsEvil.

对于我在使用React路由器和启用HTML5 pushState的类似React应用程序中的用例,其他答案不包括具有目录深度的路由。当路由在"目录"(如example.com/foo/bar/baz/213123)中加载或刷新时,我的index.html文件将引用相对路径上的js文件,并解析为example.com/foo/bar/baz/js/app.js而不是example.com/js/app.js

对于目录深度超过第一级的情况,如/foo/bar/baz,请注意@rootfiles指令中声明的目录顺序:最长的路径需要先到达,然后是下一个较浅的路径/foo/bar,最后是/foo

由于可能存在ajax请求api,以下适用于这种情况,

server {
    listen 80;
    server_name example.com;
    root /var/www/example.com;
    # Any route containing a file extension (e.g. /devicesfile.js)
    location ~ ^.+..+$ {
        try_files $uri =404;
    }
    # Any route that doesn't have a file extension (e.g. /devices)
    location / {
        try_files $uri /index.html;
    }
    # The location block above provides the shortest prefix, of length one, 
    # and so only if all other location blocks fail to provide a match, 
    # this block will be used.
    # Ajax api starts with /v1/ will be proxied
    location /v1/ {
        proxy_pass http://proxy;
    }
}

我尝试了try_files $uri /index.html;,但nginx一直抱怨rewrite or internal redirection cycle while internally redirecting to "/index.html"错误。

我喜欢Arthur Xu提出的解决方案,但通过重写所有URL简化了它,而不是没有./index.html

  location / {
    rewrite ^[^.]+$ /index.html last;
    proxy_pass http://web:8080;
    ...

上面将所有不包含.的URL重写为/index.htm'

最新更新