简体   繁体   English

NGINX hashbang重写

[英]NGINX hashbang rewrite

I'm wondering what a location or rewrite nginx directive for hashbang (#!) urls would look like. 我想知道hashbang(#!)url的位置或重写nginx指令是什么样的。 Basically routing all non hash-banged url's through the hashbang like a front controller. 基本上通过像前端控制器一样的hashbang路由所有非哈希的网址。 So: 所以:

http://example.com/about/staff

would route to 会路由到

http://example.com/#!/about/staff

I'm unclear what the best technique here would be? 我不清楚这里最好的技术是什么? Whether writing an if statement to check existence of the hashbang, or just a generic rewrite that filters all requests... 是否编写if语句来检查hashbang的存在,或者只是编写一个过滤所有请求的泛型重写...

GETs for fragment identifiers don't/shouldn't (some buggy clients may send them) appear in an HTTP request, so you can't have a rewrite rule to match them, regardless of webserver. 片段标识符的GET不会/不应该(某些错误的客户端可能会发送它们)出现在HTTP请求中,因此无论Web服务器如何,您都无法使用重写规则来匹配它们。

The HTTP engine cannot make any assumptions about it. HTTP引擎无法对其做出任何假设。 The server is not even given it. 服务器甚至没有给它。

If you tried to make the initial request for / redirect to /#! 如果您尝试将/ redirect的初始请求发送到/#! rather than serving the root index, you'd end up with a "too many redirects" error, as the client would come back asking for / again (remember that it won't send the # with its request). 而不是提供根索引,你最终会出现“太多的重定向”错误,因为客户端会再次请求/(请记住它不会发送#及其请求)。 You'll need to do this with javascript instead for the index document . 您需要使用javascript代替索引文档

The bottom line is that it's not available server-side in the GET request. 底线是它在GET请求中不可用于服务器端。 Even curl has been patched not to send it any more. 即使卷曲已被修补,不再发送它。

You could have nginx location directives to make everything else hit the front controller though: 您可以使用nginx位置指令使其他所有内容都击中前端控制器:

 location = / {
  }      

  location = /index.html {
  }      

  location ~ / {
    rewrite ^ /#!$uri redirect;
    break;
  }

Beware of this approach though; 但要注意这种方法; http://jenitennison.com/blog/node/154 goes into a lot more detail about the hashbang debacle at Gawker and other issues surrounding its use. http://jenitennison.com/blog/node/154详细介绍了Gawker的hashbang崩溃及其使用的其他问题。

As a modification of the above, I only do this redirection for specific calls, and thusly get no potential loop backs: 作为上述的修改,我只对特定的调用进行此重定向,因此没有潜在的循环:

location ~ /login|/logout|portfolios|/portfolio/*|/public/* {
  rewrite ^ /#!$uri permanent;
  break;
}

Note that this works fine on all browser except for Safari. 请注意,除了Safari之外,这适用于所有浏览器。 Safari will redirect you to the url sans the hash. Safari会将您重定向到没有散列的网址。

The best route is to use the try_files directive: 最好的方法是使用try_files指令:

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

Assuming your index.html file contains the javascript logic that routes the hash to the correct resource. 假设您的index.html文件包含将哈希路由到正确资源的javascript逻辑。 The URI will remain what the visitor requested whereas Nginx simply routes the request to your index file when no real matching file can be found for the request uri. URI将保留访问者请求的内容,而Nginx只是在没有找到请求uri的真实匹配文件时将请求路由到您的索引文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM