- Docker & Docker Compose installed
- Go 1.24.4 or higher
- Node.js 18+ and npm
- Git (for version control)
cd ./compass/server
cp secret.yml.template secret.ymlcd ./compass/server
docker-compose up postgres rabbitmqcd /compass/server
go mod tidy
go build -o server ./cmd/.
./servercd /compass
npm install
npm run devrepeat this in the /search repository
cd /compass/search
npm install
npm run dev- Intercepts all requests and checks the
X-Subdomainheader set by nginx - Routes are validated based on the subdomain
- Redirects invalid routes (e.g., accessing auth pages on maps subdomain)
- Listens on different subdomains
- Sets
X-Subdomainheader to identify the route - Proxies requests to the same Next.js frontend on port 3001
- All API requests go to port 8081
NEXT_PUBLIC_AUTH_DOMAIN=https://auth.domain.in
NEXT_PUBLIC_MAPS_DOMAIN=https://compass.domain.in
NEXT_PUBLIC_MAIN_DOMAIN=https://domain.in- Helper hook to get current domain based on route
- Provides
currentDomain,authDomain,mapsDomain,mainDomain - Detects if you're on an auth or maps route
Add to your .env.local or deployment config:
# For production
NEXT_PUBLIC_AUTH_DOMAIN=https://auth.yourdomain.com
NEXT_PUBLIC_MAPS_DOMAIN=https://compass.yourdomain.com
NEXT_PUBLIC_MAIN_DOMAIN=https://yourdomain.com
# For development/local
NEXT_PUBLIC_AUTH_DOMAIN=http://localhost:3000
NEXT_PUBLIC_MAPS_DOMAIN=http://localhost:3000
NEXT_PUBLIC_MAIN_DOMAIN=http://localhost:3000Ensure your DNS provider has records
Update the nginx config with your SSL certificate paths:
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;Or use Let's Encrypt with a wildcard certificate:
certbot certonly --standalone -d "*.yourdomain.com" -d "yourdomain.com"# Reload nginx
sudo nginx -s reload
# Rebuild Next.js (if ENV changed)
npm run build
npm start- User visits
auth.domain.in/login - Nginx receives request, identifies
authsubdomain - Nginx sets header
X-Subdomain: auth - Nginx proxies to
localhost:3001(Next.js) - Middleware reads
X-Subdomainheader - Middleware validates route matches subdomain
- Route group
(auth)renders login page
| Subdomain | Allowed Routes | Redirect Target |
|---|---|---|
auth.domain.in |
/login, /signup, /forgot-password, /reset-password, /privacy-policy, /profile |
/login |
compass.domain.in |
/location/*, /noticeboard/*, / |
/ |
domain.in |
All routes | N/A |
For local testing without DNS:
- Update
/etc/hosts(macOS/Linux):
127.0.0.1 localhost
127.0.0.1 auth.localhost
127.0.0.1 compass.localhost
-
Set environment variables to
http://localhost:3000 -
Run Next.js in dev mode:
npm run dev- Nginx will handle subdomain routing on your local machine
Solution: Check that X-Subdomain header is being set in nginx config.
proxy_set_header X-Subdomain auth;Solution: Configure CORS in your API backend for all subdomain origins:
// Example in Go/Fiber
app.Use(cors.New(cors.Config{
AllowOrigins: "https://auth.domain.in,https://compass.domain.in,https://domain.in",
AllowMethods: "GET,POST,PUT,DELETE",
}))Solution: Set cookies with domain attribute in your auth backend:
cookie := &http.Cookie{
Name: "auth_token",
Value: token,
Domain: ".domain.in", // Note the leading dot for subdomains
Path: "/",
Secure: true,
HttpOnly: true,
}
http.SetCookie(w, cookie)- Dynamic Domain Loading - Load domain config from database
- Multi-Region Support - Different subdomains for different regions
- Feature Flags - Toggle subdomain routing on/off
- Analytics - Track which subdomain users access most