Nginx和Heroku. 提供静态文件服务

4

目标:

访问https://secret-sands-1568.herokuapp.com/myFile.html将会显示 Nginx 发送的client/myFile.html文件内容。


据我理解,Heroku dynos 通常用于 Nodejs/Ruby/其他应用程序,但可以通过修改 buildrack 转换为任何类型的应用程序。

  • 我复制了https://github.com/theoephraim/nginx-buildpack的内容。
  • 我添加了一个名为 tmp 的文件夹和一个名为 app-initialized 的文件。
  • 我添加了一个名为 client 的文件夹和一个名为 myFile.html 的文件。

config/nginx.conf.erb 中,我将 location / 改为

    location / {
        root   client;
        index  index.html index.htm;
    }
  • 我创建了一个名为Procfile的文件,其内容为:web: bin/start-nginx
  • 我输入了heroku config:set BUILDPACK_URL=https://github.com/ryandotsmith/nginx-buildpack.git

  • 我依次输入了git initheroku creategit push heroku masterheroku ps:scale web=1


然而,当我访问https://secret-sands-1568.herokuapp.com/myFile.html时,什么也没有。Heroku日志显示:

2015-05-19T07:13:32.990254+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:33.995458+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:34.998228+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:36.000823+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:37.005955+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:38.008548+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:39.011184+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:40.013788+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:41.016516+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:42.019372+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:43.023551+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:44.029985+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:45.035183+00:00 app[web.1]: buildpack=nginx at=app-initialization
2015-05-19T07:13:45.910618+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2015-05-19T07:13:45.910618+00:00 heroku[web.1]: Stopping process with SIGKILL
2015-05-19T07:13:46.732519+00:00 heroku[web.1]: State changed from starting to crashed
2015-05-19T07:13:46.718745+00:00 heroku[web.1]: Process exited with status 137
2015-05-19T07:17:12.890713+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=obscure-caverns-1927.herokuapp.com request_id=6d15e849-ce45-41bc-9bad-60d9eb0454ef fwd="24.200.222.98" dyno= connect= service= status=503 bytes=
2015-05-19T07:17:13.039723+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=obscure-caverns-1927.herokuapp.com request_id=7aeecc36-05b5-4799-bf0a-6ba97b2a6190 fwd="24.200.222.98" dyno= connect= service= status=503 bytes=
2015-05-19T07:17:18.350029+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/myFile" host=obscure-caverns-1927.herokuapp.com request_id=cb77356f-ddf3-4795-b56d-fa9cde5d134a fwd="24.200.222.98" dyno= connect= service= status=503 bytes=
2015-05-19T07:17:18.471762+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=obscure-caverns-1927.herokuapp.com request_id=64a89596-b287-4b89-a3ce-ed750e4fb1c1 fwd="24.200.222.98" dyno= connect= service= status=503 bytes=
2015-05-19T07:17:22.250495+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=obscure-caverns-1927.herokuapp.com request_id=4ccda4e8-1532-49dc-8658-e67312ef333d fwd="24.200.222.98" dyno= connect= service= status=503 bytes=
2015-05-19T07:17:22.125124+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/myFile.html" host=obscure-caverns-1927.herokuapp.com request_id=33cdee81-2dda-44cb-acf7-2dc5b4156ef2 fwd="24.200.222.98" dyno= connect= service= status=503 bytes=

我做错了什么?我的目标是拥有一个独立的Nginx服务器,用于提供静态文件。

错误 R10(引导超时)-> Web 进程在启动后 60 秒内未能绑定到 $PORT,这表明 nginx 或其他一些 Web 服务器正在尝试绑定到 $PORT,但不成功.. 检查权限(例如,端口 80 或 443 只能由特权用户使用)、您的 nginx 配置以及是否已经有东西占用了该端口(netstat tulpne 应该可以解决问题)。 - VF_
1个回答

2
编辑: 专门用于提供静态文件时,您不需要经历所有这些努力。有一个名为 heroku-community/static 的构建包,比nginx更简单,而且说明适用于静态站点。我刚刚使用它让我的Angular站点正常工作了。

我知道这可能对原问题的提问者没有帮助,但它可能会帮助其他人。

注意:这只是部分答案。我仍在寻找更多的调试方法。但我成功通过以下方式将我的超时问题解决,并服务所有404页面。

还要注意,我没有使用来自github的任何内容。我使用的是https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nginx,通过heroku buildpacks:add heroku-community/nginx添加。

Procfile:

web: bin/start-nginx-solo

这应该用来提供静态文件,而不是尝试路由到另一个监听器。
到目前为止,我一直收到 nginx 错误,如下所示:open() "/dist/index.html" failed (2: No such file or directory)(这些来自app[web.1])。构建成功了,但它只是找不到任何文件。仍在努力解决这个问题,但至少现在不会因为引导超时而崩溃了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接