2014-09-29 95 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