Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Direct multiple hosts from traefik 2 to one nginx

I want to route test1.test and test2.test to different folders in an nginx php config using traefik 2. Both are PHP applications.

Here is my nginx site.conf:

server {
    listen 80;

    server_name test1.test
    root /var/www/html/test1;
}
server {
    listen 80;

    server_name test2.test
    root /var/www/html/test2;
}

and here is mij docker-compose file for the traefik container

version: "3"

services:
  traefik:
    image: "traefik:v2.3"
    container_name: "traefik"
    restart: "unless-stopped"
    security_opt:
      - no-new-privileges:true
    networks:
      - proxy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /etc/localtime:/etc/localtime:ro
      - ./traefik.toml:/traefik.toml:ro
      - ./acme.json:/acme.json
      - ./configurations:/configurations
networks:
  proxy:
    external: true
  internal:
    external: false

And the docker-compose for my applications

version: "3"

services:
  nginx:
    image: nginx:latest
    container_name: test_nginx
    links:
      - php
    volumes:
      - ./nginx/server.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/site.conf:/etc/nginx/conf.d/default.conf:ro
      - ../../src:/var/www/html
    networks:
      - proxy
      - internal
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.nginx.entrypoints=web"
      - "traefik.http.routers.nginx.rule=Host(`test1.test`) || Host(`test2.test`)"
  php:
    build: ./php
    container_name: test_php
    ports:
       - "9000:9000"
    volumes:
      - ../../src:/var/www/html
    networks:
      - internal

My hostsfile is all set and pointing to localhost (172.0.0.1)

But when browsing to test1.local it gives 404 file not found and browsing to test2.test is gives 404 not found nginx.

What I want to do is basicly have one traefik2, one nginx with multiple drupal sites after it.

Can someone explain me how to have 2 hosts pointing to same nginx and redirecting to the correct folder? Thank you.

like image 238
Kwinten Hardies Avatar asked Oct 19 '25 14:10

Kwinten Hardies


1 Answers

Update your label

"traefik.http.routers.nginx.rule=Host(\`test1.test\`) || Host(\`test2.test\`)"

to

"traefik.http.routers.nginx.rule=Host(\`test1.test\`,\`test2.test\`)"
like image 138
Sudip Mandal Avatar answered Oct 22 '25 04:10

Sudip Mandal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!