Managing Dynamic Subdomains in Spring Boot with Nginx and DNS Configuration

Managing dynamic subdomains is a crucial aspect of many web applications, especially multi-tenant platforms where each user or tenant gets their own subdomain. In this guide, I’ll show you how to handle dynamic subdomains in a Spring Boot application, configure Nginx to properly route requests, set up SSL certificates with Let’s Encrypt, and configure the required DNS records.
Let’s get started!
Step 1: Handling Dynamic Subdomains in Spring Boot
Spring Boot makes it simple to extract subdomains from incoming requests. Here’s a minimal Spring Boot controller to handle this:
@RestController
@RequestMapping("/")
public class SubdomainController {
@GetMapping
public String handleRequest(HttpServletRequest request) {
String host = request.getServerName();
String subdomain = extractSubdomain(host);
return "Hello from subdomain: " + subdomain;
}
private String extractSubdomain(String host) {
String[] parts = host.split("\\.");
return (parts.length > 2) ? parts[0] : null;
}
}
How It Works:
- The
handleRequest
method extracts the host (e.g.,tenant1.example.com
). - The
extractSubdomain
method checks if the hostname has more than two parts (e.g.,tenant1.example.com
→tenant1
).
Step 2: Configuring Nginx for Wildcard Subdomains and SSL
To make sure that Nginx properly routes traffic for any subdomain, we need to configure it with a wildcard subdomain and set up SSL certificates with Let’s Encrypt.
Update your Nginx configuration:
server {
listen 80;
server_name *.example.com example.com;
# Redirect all HTTP requests to HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name *.example.com example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'HIGH:!aNULL:!MD5';
# Custom error page
error_page 502 /502.html;
location = /502.html {
root /usr/share/nginx/html;
internal;
}
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Setting Up Let’s Encrypt SSL Certificate
To enable HTTPS for your subdomains, use Certbot to generate and manage SSL certificates.
- Install Certbot and the Nginx plugin:
sudo apt update
sudo apt install certbot python3-certbot-nginx
2. Run Certbot to generate a wildcard SSL certificate:
sudo certbot --nginx -d example.com -d "*.example.com"
3. Certbot will automatically configure SSL settings in your Nginx config.
After updating the configuration, restart Nginx:
sudo systemctl restart nginx
Step 3: Setting Up DNS Records
To ensure your subdomains resolve correctly, you need to configure DNS records with your domain provider.
Add a Wildcard DNS Record:
- Go to your DNS provider’s dashboard (e.g., Cloudflare, GoDaddy, Namecheap, etc.).
- Add a new A record:
- Type: A
- Name:
*
(wildcard subdomain) - Value: Your server’s IP address
- TTL: Auto or 3600 seconds
3. Save the record.
Now, any subdomain (e.g., tenant1.example.com
) will automatically point to your server.
Step 4: Testing Everything
After setting everything up, test your dynamic subdomains:
- Run your Spring Boot application:
mvn spring-boot:run
2. Open your browser and visit:
https://tenant1.example.com
3. You should see a response:
Hello from subdomain: tenant1
Now your application dynamically handles subdomains with SSL security.
Conclusion
In this guide, we covered how to:
Extract dynamic subdomains in Spring Boot
Configure Nginx for wildcard subdomains
Set up SSL certificates with Let’s Encrypt
Configure DNS records for subdomains
Test the setup
This approach is ideal for multi-tenant SaaS applications or any project requiring custom subdomains for users. Now, you have a scalable, secure, and efficient way to manage subdomains dynamically!