Search code examples
spring-bootnginxherokuheroku-api

Springboot application with nginx as proxy deploy on Heroku


I am trying to deploy Spring boot application with nginx as proxy on Heroku. I am able to deploy the application without any issue without nginx in the front but when I try to add nginx using https://github.com/heroku/heroku-buildpack-nginx build pack, the application gets deployed file but it crashes with below error message. Based on I read in the logs, this happens because spring boot application and nginx both trying to use the same port rather than using separate one.

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

Below is the procfile that I have in place, please note that if I remove nginx part, it works fine and application works without any issues but doesn't work if I keep nginx in between.

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

My nginx configuration is pretty much same as mentioned on buildpack. https://github.com/ryandotsmith/nginx-buildpack/blob/master/config/nginx.conf.erb location.

Can someone please help on how to fix this?


Solution

  • I was able to fix this using below changes. I have made below changes on nginx config file.

    Default changes on nginx buildpack is as below:

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

    We have changed these config file as below.

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

    Also below are the proc file changes where I have added new environment variable APPLICATION_PORT which fixed the issue for me.

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