1) Fix router/service name collision when same container serves multiple domains

This commit is contained in:
chungyan5 2026-06-26 14:43:55 +08:00 committed by chungyan5
parent 20c55e0c4b
commit 4cfd6443b9
3 changed files with 26 additions and 22 deletions

View File

@ -1277,6 +1277,8 @@
## make a generic rvs-prxy system
- In a case, i have two domains name, that means two .env-www-domain-com and .env-domain-com, their service are same to one container, say web-fe-1, i find it has one domain work, another does not. I see the reason as: it has two dynamic configuration files inside /etc/traefik/config/, say app-service-www.domain.com-web-fe-1.yml, and app-service-domain.com-web-fe-1.yml, inside them, the routers are same names, say web-fe-1-https and web-fe-1-http, the services name are same too, say web-fe-1-service. By observation, the names format of routers and services is container-name-https, container-name-http and container-name-service. So i aim to improve this name format rule, do not use container-name, it may share to diff. domains, so should use domain name inside the rule, eg. domain-name-https, domain-name-http, domain-name-service, etc.
- Currently .env-p2prly1-ivantito-com as:
```
DOMAIN_NAME="p2prly1.ivantito.com"

View File

@ -160,28 +160,30 @@ http:
```yaml
http:
routers:
<service-name>-https:
<domain>-https:
rule: 'Host(`<domain>`)'
service: <service-name>-service
service: <domain>-service
tls:
certResolver: letsencrypt
entryPoints:
- websecure
<service-name>-http:
<domain>-http:
rule: 'Host(`<domain>`)'
service: <service-name>-service
service: <domain>-service
entryPoints:
- web
middlewares:
- redirect-to-https
services:
<service-name>-service:
<domain>-service:
loadBalancer:
servers:
- url: 'http://<service-name>:<port>'
- url: 'http://<container-name>:<port>'
passHostHeader: true
```
Router, service, and middleware names use the domain (or domain-without-sub-path) as the prefix instead of the container name. This ensures unique resource names when multiple domains share the same backend container. The backend URL still references the container hostname for Docker networking.
**Entrypoints Configuration:**
Default entrypoints (`web` on :80, `websecure` on :443) are defined in the docker-compose.yml command args. Additional entrypoints can be configured via the `EXTRA_ENTRYPOINTS` environment variable in `.env.rvs-prxy`:
- Format: `name=:port/protocol,name2=:port` (comma-separated)

View File

@ -1897,30 +1897,30 @@ configure_traefik_local() {
cat > "$temp_config" << EOF
http:
routers:
${LOCAL_SERVICE_NAME}-https:
${domain_part}-https:
rule: 'Host(\`${domain_part}\`) && PathPrefix(\`${path_part}\`)'
service: ${LOCAL_SERVICE_NAME}-service
service: ${domain_part}-service
tls:
certResolver: letsencrypt
entryPoints:
- websecure
middlewares:
- ${LOCAL_SERVICE_NAME}-strip-prefix
${LOCAL_SERVICE_NAME}-http:
- ${domain_part}-strip-prefix
${domain_part}-http:
rule: 'Host(\`${domain_part}\`) && PathPrefix(\`${path_part}\`)'
service: ${LOCAL_SERVICE_NAME}-service
service: ${domain_part}-service
entryPoints:
- web
middlewares:
- redirect-to-https
- ${LOCAL_SERVICE_NAME}-strip-prefix
- ${domain_part}-strip-prefix
middlewares:
${LOCAL_SERVICE_NAME}-strip-prefix:
${domain_part}-strip-prefix:
stripPrefix:
prefixes:
- ${path_part}
services:
${LOCAL_SERVICE_NAME}-service:
${domain_part}-service:
loadBalancer:
servers:
- url: 'http://${LOCAL_SERVICE_NAME}:${first_tcp_port}'
@ -1931,22 +1931,22 @@ EOF
cat > "$temp_config" << EOF
http:
routers:
${LOCAL_SERVICE_NAME}-https:
${domain_part}-https:
rule: 'Host(\`${DOMAIN_NAME}\`)'
service: ${LOCAL_SERVICE_NAME}-service
service: ${domain_part}-service
tls:
certResolver: letsencrypt
entryPoints:
- websecure
${LOCAL_SERVICE_NAME}-http:
${domain_part}-http:
rule: 'Host(\`${DOMAIN_NAME}\`)'
service: ${LOCAL_SERVICE_NAME}-service
service: ${domain_part}-service
entryPoints:
- web
middlewares:
- redirect-to-https
services:
${LOCAL_SERVICE_NAME}-service:
${domain_part}-service:
loadBalancer:
servers:
- url: 'http://${LOCAL_SERVICE_NAME}:${first_tcp_port}'
@ -1994,12 +1994,12 @@ EOF
udp:
routers:
${LOCAL_SERVICE_NAME}-${matching_ep}:
${domain_part}-${matching_ep}:
entryPoints:
- ${matching_ep}
service: ${LOCAL_SERVICE_NAME}-udp
service: ${domain_part}-udp
services:
${LOCAL_SERVICE_NAME}-udp:
${domain_part}-udp:
loadBalancer:
servers:
- address: "${LOCAL_SERVICE_NAME}:${container_port}"