nginx支持html5,对于单页网站使用HTML5按压状态URL的nginx的配置(Configuring nginx fo

我如何配置nginx的重定向URL所有的(不带前置/api或一些静态资源如:JS /图像) index.html ? 原因是我使用HTML5按压状态URL与一个单页的应用程序。 含义的内容改变是否AJAX或JS取决于网址

我现在nginx的配置是这样的:

server {

listen 2000;

server_name localhost;

location / {

root /labs/Projects/Nodebook/public;

index index.html;

}

location /api/ {

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_set_header X-NginX-Proxy true;

proxy_pass http://localhost:3000/;

proxy_redirect off;

}

}

Answer 1:

location / {

try_files $uri /index.html;

}

这将检查所请求的文件是否存在,并将其返回。 如果该文件不存在,它会返回的index.html。

http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

Answer 2:

遮罩答案几乎是一个解决方案,但是它不会给404丢失的文件(如favicon.icon)作为aschepis指出。

Nginx的会选择第一个匹配的位置。 因此,我们可以先匹配文件(这将给404,如果该文件不存在)。 并把后默认为index.html的所有网址,其中一个位置。

location /.+\..+ { # files (assuming they always have a dot)

# use eg alias to serve some files here

}

location / { # url routed by client, client gives 404 for bad urls

try_files $uri /index.html;

}

Answer 3:

您需要添加到您的nginx的配置文件:

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

然后说你使用Backbone.js的只是确保你重新路由到一个404页的任何非自定义路线:

routes: {

// Other routes

"*path" : "notFound"

},

notFound: function(path) {

// Load 404 template, probably of a cute animal.

}

来源: http://readystate4.com/2012/05/17/nginx-and-apache-rewrite-to-support-html5-pushstate/

文章来源: Configuring nginx for single page website with HTML5 push state URL's

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值