2016-04-30 86 views
0

我有一個angularjs應用程序,它也有一個博客。該網址顯示下nginx匹配特定路徑上來自不同文件夾的特定文件

http://example.com/blog/example-blog-post-title 

http://example.com/blog/ 

所有博客文章和特定的博客帖子現在我預編譯的博客文章的搜索引擎優化目的的HTML,我想從我的主要的應用程序完全獨立地爲他們服務像這樣:

... 
root "/home/ubuntu/client/public"; 

location/{ ## Handle default requests ## 
    try_files $uri $uri/ /index.html; 
} 

location /blog { ## serve precompiled blog HTML 
    alias /home/ubuntu/bloghtml; 
    try_files $uri.html $uri/ index.html; 
} 
... 

而這個工作,通過將http://example.com/blog/example-blog-post-title nginx的成功提供文件/home/ubuntu/bloghtml/example-blog-post-title.html

但問題是,在這種情況下,nginx並沒有正確地將博客帖子列表http://example.com/blog/路由到我的主角應用程序,我得到該URL的錯誤403。

我試過的conf文件中更改location /bloglocation /blog/,這使得http://example.com/blog/工作,howewever我得到http://example.com/blog/example-blog-post-title

404錯誤,我怎樣才能使這項工作,對於兩種情況?

+0

[如果我理解正確] 如果您是URL包含/博客(例如:http://example.com/blog/some-post.html)將始終從/ home/ubuntu/bloghtml根。如果您想將請求轉發給您的角度應用程序,您可能需要爲其拍攝不同的位置指令。 – RaviTezu

回答

1

如果將位置從/blog更改爲/blog/,則需要記住將別名從/home/ubuntu/bloghtml更改爲/home/ubuntu/bloghtml/。別名和位置需要有相同的結尾,否則計算的路徑名是錯誤的。

由於some known issues,我儘量避免在同一個塊中使用aliastry_files。您可以考慮在路徑blog中創建最後一個目錄,以便您可以改爲使用root


我相信,你的角度應用程序是/index.html,在這種情況下,你try_files說法是不正確。 $url/將使其嘗試/blog/index.html(假設您有一個index指令生效)並且index.html缺少領先的/

我建議你試試:

location /blog { 
    alias /home/ubuntu/bloghtml; 
    try_files $uri.html /index.html; 
} 

但考慮太設計出alias指令。

相關問題