8

I have a spring boot application. Usually I run my Spring applications on PaaS instances, and configuring a domain name from there is easy enough, however I am running this on a Virtual Private Server, and I cannot, for the life of me, figure out how to run my spring boot so it's accessible with a domain name.

I have already changed my DNS settings so it's pointing to my Virtual Private Server, this VPS also runs some other apache based static websites, I'm pretty confident my DNS settings are correct.

My spring boot application is running using spring-boot-starter-tomcat, the application deploys fine, I can grab my .war file and deploy it using java -jar myApplication.jar on the server.

The application is also accessible remotely by writing my.server.ip:8080 on a browser.

However, I've been googling a lot and cannot figure out how to configure Spring Boot so that it'll use my Domain name, so that I can access the website in a standard way: www.mywebsite.com, or even better yet also add an Alias so both mywebsite.com and www.mywebsite.com are valid.

Can anyone point me in the right direction? I know this can be done in Tomcat but I have no idea of how to configure it.

Since this is a Spring Boot application I do not have .xml files, my Spring Boot configuration is in a application-prod.yml file, and the only .xml file I use is the pom.xml itself.

Any help would be greatly appreciated.

Erick
  • 2,488
  • 6
  • 29
  • 43

2 Answers2

9

Due to the lack of responses I decided to go with the approach proposed by dunni.

Here's how I did it using Nginx:

I went to a clean windows installation nginx/conf/nginx.conf

Then I changed some stuff around, here is my complete nginx.conf file:

nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    include C:/path-to-nginx/nginx/conf/sites-enabled/*.conf;
}

I then created the folder sites-enabled in nginx/conf/sites-enabled

I proceeded to create mywebsite.conf inside the folder sites-enabled:

mywebsite.conf

server {
    server_name  mywebsite.com;

    location / {
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080;
    }
}

At this point if everything was done properly you should be able to access your Tomcat application! I used a lot of references, so I'll drop them all down below:


http://www.mkyong.com/nginx/nginx-apache-tomcat-configuration-example/

http://javadeveloper.asia/configuring-nginx-in-front-of-tomcat-or-other-java-application-server

http://nginx.org/en/docs/windows.html

nginx Windows: setting up sites-available configs

Cheers.

Erick
  • 2,488
  • 6
  • 29
  • 43
  • 1
    I thought this was going to break the site I had hosted on the same VPS, but it didn't! Awesome! – GuiRitter Oct 10 '18 at 13:12
  • 1
    Hello Erick, you need to start apache with root permissions in order to use port 80 and 443. And to redirect http to https you can have a look here. However HTTP/2 cannot be activated using spring boot 1.5. Here is the link.https://memorynotfound.com/spring-boot-configure-tomcat-ssl-https/ – sam Mar 02 '19 at 18:43
  • 1
    But if you want the HTTP/2 feature as well then switch to Undertow server instead of Tomcat. Another point to be noted is undertow has better throughput and is low on memory which is a big plus over Tomcat. Here you go https://stackoverflow.com/a/54908727/7538821 – sam Mar 02 '19 at 18:45
  • 1
    If shared hosting is not your case then, instead of setting a standalone tomcat server you can use embedded servers too as they are also production grade. – sam Mar 02 '19 at 18:56
6

Technically you could do that in Tomcat. However, to start the application with port 80 or 443 you would have to run it with root permissions. Thus i'd recommend to configure and Apache HTTP or an Nginx server as reverse proxy (you can find many tutorials for that topic).

dunni
  • 43,386
  • 10
  • 104
  • 99
  • I can change the port, if you can elaborate as to how to do it with Tomcat itself I'd be very grateful. I also have root access, so that's a non-issue. – Erick Jun 12 '15 at 05:52
  • The problem isn't having root access. But you should never start your application with root permissions. If you have a security breach, the attacker can execute commands with the permissions of the application, so if the app is running with root permissions, the attacker also have root permissions on your server. – dunni Jun 12 '15 at 07:56
  • Fair enough! Went with your approach. And it all does work, no complaints. Have a good one! – Erick Jun 12 '15 at 07:57