How to set Domain name in Spring Boot App with embedded tomcat

Viewed 26373

I am developing a application in spring boot with embedded tomcat. In my local which runs on a port 8080 and i can give url http://locahost:8080. How can change this to my domain? like www.mydomain.com which should work similar to localhost. How to configure this? I am using embedded tomacat not externally installed tomcat server.

3 Answers

First of all you need to have a domain registered.

Then you need to have a Machine in premise or in the Cloud whose Public IP address is mapped to the domain you registered, and that has the correct port (80) opened.

Then you need to start your Spring boot application to run on port 80 not 8080. You can do that by using CLI argument --server.port=80 or adding server.port=80 in application.properties file or application.yaml file.

You don't have to specify the domain name anywhere in your application.

In the SpringBoot project open the application.properties file(under src/main/resources)

And configure the port on which you want to run your application using

server.port = XXXX

where XXXX is the port number.(80 if you don't want to provide the port while accessing the application)

The only extra configuration that needs to be done is to update DNS to point mydomain.com to IP address of your machine. For now, since you are using your local machine, you can test whether redirect works by editing your hosts file (C:\Windows\System32\drivers\etc) to keep this mapping.

NOTE: This editing will enable you to test only if you are accessing the domain from your machine only.

If you are deploying this spring boot application as your primary service and not running it on a server that has Apache Web Server already installed, you can manually set the port 80, which is for HTTP requests. 443 is encrypted and thus HTTPS. You can set these settings on your firewall of your server.

However, if this Spring boot app happens to be something like an API, where it's just endpoints you want to hit from a website that you have on your server (running on something like an Apache Web Server), you're going to need to setup up a reverse proxy or else they will both be trying to use port 80:

https://medium.com/@codebyamir/using-apache-as-a-reverse-proxy-for-spring-boot-embedded-tomcat-f704da73e7c8

So you should leave the port as 8080 on the Spring app (running the embedded tomcat server), and your Apache Web Server should be using port 80 for say , your website at www.mydomain.com.

Thus, the proxy will redirect incoming HTTP requests to your Tomcat service at port 8080 and thus the endpoints will be triggered via www.mydomain.com/api-end-point-here

Related