Search code examples
javaspring-bootdockerspring-mvcamazon-ec2

How can I multiple two spring boot web applications on same port with different endpoints?


I have two spring boot web applications. I want those applications to be deployed on same port with different endpoints, like

  • Application 1 - xx.xx.xx.xx:80/app1
  • Application 2 - xx.xx.xx.xx:80/app2

I am trying to deploy these applications using docker in AWS EC2. Please let me know how to do this.

Thank you!

When I tried to deploy two applications on same port, it failed.


Solution

  • Having two apps running on the same port on a single machine is impossible. However, you can run applications on each port and combine them using Nginx. Example:

    • Application A (/app1) is running on 8080

    • Application B (/app2) is running on 8081

    • And the below Nginx Config might cover your case, I guess.

      listen 80;
      
       location /app1 {
           proxy_pass http://localhost:8080;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }
      
       location /app2 {
           proxy_pass http://localhost:8081;
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
       }