Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot configure a Domain/Host to access in a www.website.com fashion

Tags:

nginx

tomcat

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.

like image 815
Erick Avatar asked Jun 12 '15 05:06

Erick


2 Answers

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.

like image 100
Erick Avatar answered Nov 08 '22 16:11

Erick


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).

like image 22
dunni Avatar answered Nov 08 '22 15:11

dunni