简体   繁体   English

Spring Boot 中的子域路由

[英]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)... domain.xyzsomething.domain.xyz都指向同一个服务器,然后 Spring 应用程序在路由时考虑子域,这样我就可以在顶级域上有不同的内容,在子域上有不同的内容......

Is this possible to achieve with Spring Boot?这可以通过 Spring Boot 实现吗?

(Note: This is not 100% tested, but will probably work) (注意:这不是 100% 测试,但可能会工作)

I'm assuming you are having an Nginx or Apache in front of your Spring Boot application.我假设您的 Spring Boot 应用程序前面有一个 Nginx 或 Apache。

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.以 Nginx 为例,您可以使用 proxy_pass 指令,然后在转发到 Spring Boot 应用程序时将“Host”标头设置为“something.domain.xyz”或“domain.xyz”。

You could therefore enhance your GetMappings to filter for the Host header values .因此,您可以增强 GetMappings 以筛选 Host 标头值

@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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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