Use Traefik reverse-proxy to host WordPress and Django

Viewed 232

I am new to deployment related tasks so what I'm asking may be very simple to implement but it is difficult for me nonetheless.

In one domain, I need a WordPress-based public website (about 3-4 pages) as well as a link to log into the internal management Django v3.2 app. Both plus some extras (redis, postgres) will be served through docker-compose.

I am using cookiecutter-django for my Django application. It uses Traefik by default as reverse-proxy for the production configuration. I looked at the docs for Traefik and I couldn't quite understand how I can implement the paths for my use case. I have the WP site and the Django app separately working currently so I just need to figure out how to deploy it together under the same domain, just different paths.

As an example of what I need to achieve, let's assume my WordPress site has pages home, about, and contact so I would want the full URL to be example.com, example.com/about, and example.com/contact respectively.

Any other links should be forwarded to Django.

For those who have not used cookiecutter-django, the default file structure and configuration is to have a separate traefik.yml and Dockerfile for Traefik, and separate Dockerfiles for Django and Postgres plus any other extras.

I have not yet put the WP into docker-compose but I can do that easily enough. I just need to understand how I would ask Traefik to carry the requests to the appropriate container.

Here's the production.yml generated by cookiecutter-django:

version: '3'

volumes:
  production_postgres_data: {}
  production_postgres_data_backups: {}
  production_traefik: {}

services:
  # Let's assume I'll have a wordpress container here, and its dependencies (db, etc)

  django:
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    image: {{project_slug}}_production_django
    depends_on:
      - postgres
      - redis
    env_file:
      - /var/.envs/.django
      - /var/.envs/.postgres
    command: /start

  postgres:
    build:
      context: .
      dockerfile: ./compose/production/postgres/Dockerfile
    image: {{project_slug}}_production_postgres
    volumes:
      - production_postgres_data:/var/lib/postgresql/data:Z
      - production_postgres_data_backups:/backups:z
    env_file:
      - /var/.envs/.postgres

  traefik:
    build:
      context: .
      dockerfile: ./compose/production/traefik/Dockerfile
    image: {{project_slug}}_production_traefik
    depends_on:
      - django
    volumes:
      - production_traefik:/etc/traefik/acme:z
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"

  redis:
    image: redis:6

And the traefik.yml is as follows:

log:
  level: INFO

entryPoints:
  web:
    # http
    address: ":80"
    http:
      # https://docs.traefik.io/routing/entrypoints/#entrypoint
      redirections:
        entryPoint:
          to: web-secure

  web-secure:
    # https
    address: ":443"

certificatesResolvers:
  letsencrypt:
    # https://docs.traefik.io/master/https/acme/#lets-encrypt
    acme:
      email: "support@example.com"
      storage: /etc/traefik/acme/acme.json
      # https://docs.traefik.io/master/https/acme/#httpchallenge
      httpChallenge:
        entryPoint: web

http:
  routers:
    web-secure-router:
      rule: "Host(`example.com`) || Host(`www.example.com`)"
      entryPoints:
        - web-secure
      middlewares:
        - csrf
      service: django
      tls:
        # https://docs.traefik.io/master/routing/routers/#certresolver
        certResolver: letsencrypt

  middlewares:
    csrf:
      # https://docs.traefik.io/master/middlewares/headers/#hostsproxyheaders
      # https://docs.djangoproject.com/en/dev/ref/csrf/#ajax
      headers:
        hostsProxyHeaders: ["X-CSRFToken"]

  services:
    django:
      loadBalancer:
        servers:
          - url: http://django:5000

providers:
  # https://docs.traefik.io/master/providers/file/
  file:
    filename: /etc/traefik/traefik.yml
    watch: true

And the Dockerfile of Traefik is:

FROM traefik:v2.5.3
RUN mkdir -p /etc/traefik/acme \
  && touch /etc/traefik/acme/acme.json \
  && chmod 600 /etc/traefik/acme/acme.json
COPY ./compose/production/traefik/traefik.yml /etc/traefik

So how would I achieve the routing I need?

Thanks for helping!

0 Answers
Related