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

时间:2018-04-16 10:47:40

标签: spring-boot nginx heroku heroku-api

我正在尝试使用nginx作为Heroku的代理部署Spring启动应用程序。如果没有nginx,我可以在没有任何问题的情况下部署应用程序但是当我尝试使用https://github.com/heroku/heroku-buildpack-nginx构建包添加nginx时,应用程序将获得部署文件,但它会崩溃并显示以下错误消息。基于我在日志中读到的,这是因为spring boot应用程序和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

下面是我所拥有的proc文件,请注意,如果我删除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位置。

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

1 个答案:

答案 0 :(得分:0)

我可以使用以下更改修复此问题。我在nginx配置文件中进行了以下更改。

nginx buildpack的默认更改如下:

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

我们已经更改了这些配置文件,如下所示。

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

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

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