0

我有一個AngularJs應用程序(v.1.5.7),它有像www.green.com/,www.green.com/landing,www.green這樣的明確路線。 com/home等,...我試圖使用Staticfile_buildpack在PCF中部署angularjs應用程序。我有一個內容爲「root:application」的自定義Staticfile。我在「應用程序」目錄中有我的index.html,app.js和其他angularjs控制器。適用於部署在Pivotal Cloud Foundry上的angularjs應用程序的Nginx.conf

我angularJs配置具有以下路由配置,

$routeProvider 
.when("/landing", { 
     templateUrl: "landing/landing.html", 
     controller: 'LandingController' 
    }) 

在默認nginx的配置,我的基本URL(www.green.com/)&其他內部路由(如/登陸通過NG-路線& $ location.path)被正確地服務。但是,當我刷新着陸頁(www.green.com/landing)時,它給了我一個404 Not Found錯誤。我知道nginx正試圖在這裏找到名爲「landing」的目錄。

在頁面刷新的情況下,我嘗試通過自定義nginx.conf將請求重定向回我的基本URL,以便我的index.html將被加載。 我現在nginx.conf看起來像下面,

http{ 
    server { 
     listen <%=ENV["PORT"] %>; 

     index index.html; 

     location /landing { 
      rewrite^http://$host? permanent; 
     } 

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

當我試圖加載我的網站,我得到一個500內部服務器錯誤&在PCF日誌下面的錯誤線。

[error] 54#0: *2 rewrite or internal redirection cycle while internally redirecting to "/index.html", 

但是當我試圖加載www.green.com/landing它成功地重定向到我的基地URL,然後給了我一個500內部服務器錯誤。

我試圖nginx.conf

http{ 
    server { 
     listen <%=ENV["PORT"] %>; 

     index /application/index.html; 

     rewrite "^/landing" /application/index.html last; 
    } 
} 

以下但我得到404 Not Found錯誤,在PCF日誌下面的錯誤,

[error] 49#0: *3 open() "/home/vcap/app/nginx/html/application/index.html" failed (2: No such file or directory), 

請幫我創建一個自定義的工作nginx的.conf文件在這種情況下。 在此先感謝!

+0

你應該改寫你的URL在配置,對不對? –

+0

我對Angular不熟悉,但它聽起來像要啓用推送狀態路由。 https://docs.pivotal.io/pivotalcf/1-10/buildpacks/staticfile/index.html#pushstate –

+0

謝謝丹尼爾。我一定會嘗試推送狀態路由。 –

回答

0

如果你已經在你的代碼啓用html5Mode,你需要設置基地標記與該索引file.along,

server { 
    server_name my-app; 

    index index.html; 

    root /path/to/app; 

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

這是角部位Nginx的重新寫入。

+0

我在我的索引文件中設置了基本標記。 但是根據你的nginx.conf,我正在通過靜態文件設置我的根,正如我在我的問題中提到的。但我不確定爲什麼這些文件試圖從「/ home/vcap/app/nginx/html /」 –

+0

提供,您可能正在將您的資產文件夾上傳到'/ home/vcap/app/nginx/html /'對? –

+0

我已啓用html5mode。我通過禁用html5mode解決了發佈的問題,即通過刪除'$ locationProvider.html5Mode(true);' 通過此更改,我的URL顯示爲www.green.com/#/landing,並且它不會拋出404用於頁面刷新。我可以暫時保留下來。但我想了解有關Pivotal Cloud Foundry Staticfile_buildpack的nginx配置。請繼續分享您的想法。現在我將Bheema的回答標記爲答案。 –

相關問題