【问题标题】:Nginx.conf for angularjs application deployed on Pivotal Cloud Foundry用于部署在 Pivotal Cloud Foundry 上的 angularjs 应用程序的 Nginx.conf
【发布时间】:2018-01-11 22:37:08
【问题描述】:

我有一个 AngularJs 应用程序 (v.1.5.7),它有明确的路线,如 www.green.com/ 、 www.green.com/landing 、 www.green.com/home 等……我'我尝试使用 Staticfile_buildpack 在 PCF 中部署 angularjs 应用程序。我有一个自定义静态文件,其内容为“根:应用程序”。我在“应用程序”目录中有我的 index.html、app.js 和其他 angularjs 控制器。

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

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

使用默认的 nginx 配置,我的基本 url (www.green.com/) 和其他内部路由(例如 /landing 通过 ng-route 和 $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;
        }
    }   
}

当我尝试加载我的网站时,我在 PCF 日志中收到 500 内部服务器错误和以下错误行。

[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 文件。 提前致谢!

【问题讨论】:

标签: angularjs nginx static-files cloud-foundry


【解决方案1】:

如果您在代码中启用了html5Mode,则需要在索引文件中设置基本标签。以及,

server {
    server_name my-app;

    index index.html;

    root /path/to/app;

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

这是 Angular 网站的 Nginx 重写。

【讨论】:

  • 我在我的索引文件中设置了基本标签 。但是,根据您的 nginx.conf,我正在通过我在问题中提到的静态文件设置我的根目录。但我不确定为什么文件试图从“/home/vcap/app/nginx/html/”
  • 您可能正在将您的资产文件夹上传到/home/vcap/app/nginx/html/ 对吧?
  • 我已启用 html5mode。我通过禁用 html5mode 解决了发布的问题,即通过删除 $locationProvider.html5Mode(true); 进行此更改后,我的 url 看起来像 www.green.com/#/landing 并且它不会为页面刷新抛出 404。我可以暂时活下去。但我想了解有关 nginx 配置 wrt Pivotal Cloud Foundry Staticfile_buildpack。请继续分享您的想法。现在我将 Bheema 的回复标记为答案。
猜你喜欢
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多