16

I'm trying to deploy a very simple Spring Boot application on AWS Elastic Beanstalk using AWS's Java configuration (not their Tomcat configuration), but I keep getting a 502 error with the following log:

2016/06/10 02:00:14 [error] 4921#0: *1 connect() failed 
(111: Connection refused) while connecting to upstream, client: 38.94.153.178,   
server: , request: "GET /test HTTP/1.1", upstream:   "http://127.0.0.1:5000/test",
host: "my-single-instance-java-app.us-east-1.elasticbeanstalk.com"

I've tried setting my port via Spring's application.properties to what the log seems to want (5000, using server.port=5000) and have verified that my application runs successfully on that port on localhost.

This question is very similar, except that I'm deploying a JAR instead of a WAR. It seems like there is something I'm missing regarding configuring Nginx, and I don't know how to proceed.

Here's my Spring Application:

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @RestController
    public static class MainController {

        @RequestMapping("/test")
        public String testMethod() {
            return "Method success!";
        }
    }
}
Community
  • 1
  • 1
Scott Storch
  • 794
  • 3
  • 9
  • 16

5 Answers5

21

Nginx doesn't know on which port your spring boot applicaiton is running. Make application run on port 5000 that Nginx redirects to by default by adding "server.port=5000" to application.properties or other suggested ways in the last step:

https://pragmaticintegrator.wordpress.com/2016/07/12/run-your-spring-boot-application-on-aws-using-elastic-beanstalk/

Dmitry
  • 557
  • 5
  • 12
7

From your question description and the security group settings you send me your only inbound Port 80 for your EC2 instance was open to world through firewall and you were using port 5000 for your application. So using the security rule that I gave you it opened the inbound port 5000 too for your EC2 instance so your application started working without above error.

Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
  • Isn't the traffic supposed to come into the server through port 80 and `nginx` forwards the traffic to the embedded application server on port 5000? It seems like you wouldn't want to expose port 5000. I wonder if this would work if you added a rule for port 5000 with "127.0.0.1/0". There is no mention of this on http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/security-group-rules-reference.html – Snekse Jun 10 '17 at 01:53
1

If somebody is still getting such error, after setting server.port=5000. Here's what you can do if you got db connectivity:

  1. In configuration section of your dashboard, go to RDS section and create a remote db. Remember username, password of formed db.

  2. In Software section, add following properties:

SPRING_DATASOURCE_URL=jdbc:mysql://(your-db-url)/ebdb

SPRING_DATASOURCE_USERNAME=(username)

SPRING_DATASOURCE_PASSWORD=(password)

SPRING_JPA_HIBERNATE_DDL_AUTO=update

SPRING_JPA_DATABASE_PLATFORM=org.hibernate.dialect.MySQL5Dialect

1

After reading both eb-engine.log as well nginx/error.log, the default nginx port is 5000 whereas the default port for spring-boot is 8080.

We can update our application via the elastic beanstalk CLI with the setenv cmd.

eb setenv SERVER_PORT=5000

We can update this in the code via application.properties

server.port=5000
Jghorton14
  • 724
  • 1
  • 8
  • 25
0

These steps worked for me,

  1. Go-To > Environments > Select your environment,
  2. Select "Configuration" at the left
  3. Click on "Edit" against Software
  4. Scroll Down to "Environment Properties"
  5. add one more entry SERVER_PORT and set some number, I set 5000

enter image description here

Karthik H
  • 1,267
  • 12
  • 13