简体   繁体   中英

Subdomain routing in Spring Boot

Basically, what I'm trying to achieve is something like this

@GetMapping("domain.xyz")
public String getHomepage() {
    [...]
    return "homepage/main.html";
}

@GetMapping("something.domain.xyz")
public String getSubdomainHomepage() {
    [...]
    return "homepage/subdomain.html";
}

Both domain.xyz and something.domain.xyz are pointed to the same server and the Spring app then considers the subdomain when routing so I can have different content on the top level domain and different content on the subdomain(s)...

Is this possible to achieve with Spring Boot?

(Note: This is not 100% tested, but will probably work)

I'm assuming you are having an Nginx or Apache in front of your Spring Boot application.

With Nginx for example, you would use the proxy_pass directive and then set the "Host" header to your "something.domain.xyz" or "domain.xyz" when forwarding to your Spring Boot app.

You could therefore enhance your GetMappings to filter for the Host header values .

@GetMapping(value ="/", headers="Host=domain.xyz")
public String getHomepage() {
    [...]
    return "homepage/main.html";
}

@GetMapping(value ="/", headers="Host=something.domain.xyz")
public String getSubdomainHomepage() {
    [...]
    return "homepage/subdomain.html";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM