How to serve mulitple web applications on different subdomains?

Viewed 597

Not sure if the title is well suited but I am having three apps (static content) at the moment:

  • public
  • app
  • admin

public is just content which is just for the regular web, app is content which is rendered for mobile devices and admin is basically a tool for administrative stuff. They are all contained in the same Spring Boot application and talk to the REST API at example.com/api. Technically, I could just place them into resources/static such that

resources/static/public
resources/static/admin
resources/static/app

which would allow me to access the apps as such:

example.com/public/index.html
example.com/admin/index.html
example.com/app/index.html

However, my goal is to have the following structure:

      example.com     // For public
admin.example.com     // For admin
  app.example.com     // For app
      example.com/api // REST API

How can this be achieved or what can I do to make this possible?

2 Answers

It sounds like you have three systems: public, admin and app sharing a common REST api. Probably the best approach would be to serve the static resources for each of the three systems using a content delivery network (CDN) like AWS CloudFront or Google Cloud CDN.

Another approach, though less desirable, would be to use a proxy to redirect app requests (for example https://admin.example.com to https://internal-spring-boot-server/admin). You could use Apache mod_proxy with reverse proxy (see https://httpd.apache.org/docs/2.4/mod/mod_proxy.html), or NGNX reverse proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

I think that there are two ways to achieve what you are trying to do you.

Either you will configure your subdomains in your DNS plesk/cpanel/.. to look a specific Document root (The path to the each of your apps home directory.) for example when someone asks for example.com the request would ask for example.com/main, when someone asks for admin.example.com the request would ask for admin.example.com/admin etc. In this way, you essentially ignoring the subdomain in your Spring app and you manipulate the routes in the @Controller level using the RequestMapping values main/admin/etc..

Alternatively if you don't want to mess with the Provider's control panel, you could follow the steps of this answer. But then you'd have to implement these three custom RequestCondition classes and it may not be the most simple way to go.

Not the best documented answer but I hope I pointed you to the right direction.

Related