2014-09-29 94 views
4

我使用Nginx的服务于SPA(单页应用程序),以支持HTML5历史API我有所有的更深的路线改写回/index.html,所以我跟着this article它的工作原理!这是我放在nginx.conf现在:Nginx的:如何让重写规则忽略的文件或文件夹

server { 
    listen 80 default; 
    server_name my.domain.com; 
    root /path/to/app/root; 

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

但是有一个问题,我在根目录下的/assets目录包含了所有的CSS,JS,图片,字体的东西,我不想重写这些网址,我只是想忽略这些资产,我想怎么做?

+0

使用不同的'location's – 2014-09-29 06:17:41

+0

@AlexeyTen我试过,但我不明白的是,如果我用'/ assets'一个奉献的位置,什么指令(S)我应该把这个位置块? – nightire 2014-09-29 06:38:00

回答

12

rewrite分为一个location,并使用其他location作为资产/动态URL /等。

server { 
    listen 80 default; 
    server_name my.domain.com; 
    root /path/to/app/root; 

    location/{ 
     rewrite^/index.html break; 
    } 

    location /assets/ { 
     # Do nothing. nginx will serve files as usual. 
    } 
} 
+1

哇,我真的不知道“什么都不做”会做魔术! 我也尝试了一个更复杂的正则表达式'rewrite^/(?! assets | index.html)/ last;',这也适用,但我喜欢你的解决方案,因为它很干净,易于理解。 谢谢! – nightire 2014-09-29 06:47:22