In this blog we ‘ll learn about domain name mapping for spring boot api’s hosted on aws EC2. We generally see if we deploy our application on EC2 or any other service of AWS. We ‘ll able to call them by a specific IP or a DNS URL. but what if we have our domain name with us and we want to access our api’s using domain name.
If you want to do the same, So this blog is for you. In this we ‘ll see a step by step guide for mapping of spring boot api’s to custom domain name.
In our last blog we have already cover how can we deploy our spring boot application on EC2 instance of AWS. We ‘ll continue from there. If you haven’t see that blog you can checkout using this URL Spring boot app deployment to EC2 in less then 5 minutes.
Once our application deploy on EC2 instance we can access that using Public IP v4 address:Port Number of spring boot app. But now we want to access that using domain name like http://mydomain.com or whatever is your domain name.
So let’s start i am assuming you are having some domain name with you and you are the owner of that. You can purchase domain from GoDaddy or any other domain provider you like. If you don’t want to purchase a domain and need a domain for testing only so you can use website like duckDNS that provide free domain. But it’s not recommended for production, it’s good for testing your application.
Let’s start step by step now.
Domain DNS mapping with public IP of EC2
We have a domain on GoDaddy. If you don’t have no need to worry we ‘ll show you how you can get a free domain name from Duck DNS. If you have domain from GoDaddy or any other domain provider just open that.
In GoDaddy console just click on
- your profile on top right corner.
- Then click on My Products.
- Under Domains Section click on DNS button of your domain.
- Click on Add a new record.
- Fill the Type as A , Name as whatever you like it ‘ll become your subdomain, and put EC2 public IP in value section.
- Click on Save button.
It ‘ll take some time around 10 minute to update the DNS record. You can check using nslookup command or visit dnschecker website.






Get Free domain name from DuckDNS
- Go to duck DNS website https://www.duckdns.org/
- Login using Google, GitHub or any option you like.
- After login you ‘ll go to Home page. Here you ‘ll see domains section just write name of your choice and click on add domain button. If it’s available you ‘ll see on that screen.
- After updating current IP of Duck DNS domain name with your EC2 public IP v4 it ‘ll take some time for around 10 minute to update the domain DNS. You can check it on DNS Checker website https://dnschecker.org/
- Once you see the DNS updated just hit the URL of your domain: Spring boot port. Our domain name is http://javadream.duckdns.org and our spring boot application is running on port 8080 and we have expose one GET rest endpoint /test. So our full URL ‘ll be http://javadream.duckdns.org:8080/test
- Just hit http://javadream.duckdns.org:8080/test and you ‘ll get the response. Make sure your inbound rule have port 8080 open as mentioned in last blog.



Now you are wondering still we need to add port 8080 with our domain name. It does not looks good and showing bad impression. Let’s see how can we remove this port 8080 and call our API’s from domain name only.
Remove Spring boot running port from domain name
For removing 8080 port we have to put some kind of proxy that forward our domain name to spring boot running app. So for this we use the Nginx server.
Install the Nginx server on your EC2 instance using below commands.
sudo dnf install nginx -y
Enable and start the Nginx server using below command.
sudo systemctl enable nginx
sudo systemctl start nginx
Now it’s time to see if Nginx installed and running successfully or not. For that run the below command.
sudo systemctl status nginx
If everything goes good. You ‘ll get the output of above command like below screenshot.

Now we need to configure the Nginx server to proxy our request to spring boot application running port. For that we need to create a config file inside the below location.
/etc/nginx/conf.d/
Run below command to configure new file
sudo nano /etc/nginx/conf.d/javadream.conf
It ‘ll open a editor and we need to put below config in that file. After putting the below config press Ctrl+O then press enter then press Ctrl+X.
server {
listen 80;
server_name javadream.vasu.com;
location / {
proxy_pass http://127.0.0.1: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;
}
}Code language: PHP (php)
after putting the configuration just check the config status and restart the nginx server. Run the below command to do this.
sudo nginx -t
sudo systemctl restart nginx

Now hit the URL without the 8080 port. You ‘ll get the response.
Note: Make sure you have port 80 enable in your EC2 inbound rule.


Hope you enjoy this blog. If you like this please share with your friends and help us to grow. Please share your feedback in our comment sections.
If you want us to cover blog on specific topic please mention that in our comment section we ‘ll try to cover that in our upcoming blog.
Thanks
Other blogs you may like
HTTP to HTTPS redirection using Nginx server
Deploy spring boot application on EC2 AWS in less then 5 minutes.