使用 nginx 作为代理部署在 Heroku 上的 Spring Boot 应用程序

Springboot application with nginx as proxy deploy on Heroku

我正尝试在 Heroku 上部署 Spring 引导应用程序,并将 nginx 作为代理。我能够在前面没有 nginx 的情况下毫无问题地部署应用程序,但是当我尝试使用 https://github.com/heroku/heroku-buildpack-nginx 构建包添加 nginx 时,应用程序获得了已部署的文件,但它崩溃并显示以下错误消息。根据我在日志中的阅读,发生这种情况是因为 spring 启动应用程序和 nginx 都试图使用相同的端口而不是使用单独的端口。

2018-04-14T17:09:24.427513Z app[web.1]: buildpack=nginx at=nginx-start
2018-04-14T17:09:24.440105Z app[web.1]: nginx: [emerg] bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:24.440137Z app[web.1]: 2018/04/14 17:09:24 [emerg] 159#0: bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:24.940239Z app[web.1]: nginx: [emerg] bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:24.940285Z app[web.1]: 2018/04/14 17:09:24 [emerg] 159#0: bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:25.440529Z app[web.1]: nginx: [emerg] bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:25.440569Z app[web.1]: 2018/04/14 17:09:24 [emerg] 159#0: bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:25.940679Z app[web.1]: nginx: [emerg] bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:25.940746Z app[web.1]: 2018/04/14 17:09:24 [emerg] 159#0: bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:26.440833Z app[web.1]: nginx: [emerg] bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:26.440876Z app[web.1]: 2018/04/14 17:09:24 [emerg] 159#0: bind() to 0.0.0.0:10949 failed (98: Address already in use)
2018-04-14T17:09:26.940976Z app[web.1]: nginx: [emerg] still could not bind()
2018-04-14T17:09:26.941031Z app[web.1]: 2018/04/14 17:09:24 [emerg] 159#0: still could not bind()
2018-04-14T17:09:26.941552Z app[web.1]: buildpack=nginx at=exit process=nginx
2018-04-14T17:09:26.946943Z system[web.1]: Process exited (exit status 1)
2018-04-14T17:09:26.965949+00:00 system[web.1]: State changed from up to crashed

下面是我已有的 procfile,请注意,如果我删除 nginx 部分,它工作正常并且应用程序工作没有任何问题,但如果我在两者之间保留 nginx 则不起作用。

web: bin/start-nginx java -D server.port=$PORT -jar target/some-test-0.0.1-SNAPSHOT.jar

我的 nginx 配置与 buildpack 上提到的几乎相同。 https://github.com/ryandotsmith/nginx-buildpack/blob/master/config/nginx.conf.erb 位置。

有人可以帮忙解决这个问题吗?

我能够使用以下更改解决此问题。我对 nginx 配置文件进行了以下更改。

nginx 构建包的默认更改如下:

upstream app_server {
    server unix:/tmp/nginx.socket fail_timeout=0;
}

我们已将这些配置文件更改如下。

upstream app_server {
   server localhost:<%= ENV["APP_PORT"] %> fail_timeout=0;
}

下面还有我添加了新环境变量 APPLICATION_PORT 的 proc 文件更改,这为我解决了问题。

web: bin/start-nginx java -Dserver.port=$APP_PORT -jar target/dashery-complete-0.0.1-SNAPSHOT.jar