Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hosting with traefik + sub-path README #266

Closed
incaseoftrouble opened this issue Dec 18, 2020 · 14 comments
Closed

Hosting with traefik + sub-path README #266

incaseoftrouble opened this issue Dec 18, 2020 · 14 comments
Labels
enhancement New feature or request

Comments

@incaseoftrouble
Copy link

Hi,

It took me quite some time to figure out how to host this under a sub-path, so I thought I might share this info for others; maybe you can integrate it into the README / examples.

My setup is docker-compose / traefik / apache / recipes. Swapping out apache for nginx should be straightforward.

Relevant parts:

docker-compose:

  apache:
    # omitting other config
    volumes:
      - ./recipes/static:/var/www/recipes/static:ro
      - ./recipes/media:/var/www/recipes/media:ro
    labels:
      traefik.enable: true
      traefik.http.routers.apache-recipes.rule: Host(`<host>`) && PathPrefix(`/<www path>`)
      traefik.http.routers.apache-recipes.entrypoints: http
      traefik.http.routers.apache-recipes.service: apache
      traefik.http.services.apache.loadbalancer.server.port: 80
      traefik.http.services.apache.loadbalancer.server.scheme: http
...

  recipes:
    volumes:
      - ./recipes/static:/opt/recipes/staticfiles:rw
      - ./recipes/media:/opt/recipes/mediafiles:rw
    environment:
      # all the other env
      - SCRIPT_NAME=/<sub path>
      - STATIC_URL=/<www path>/static/
      - MEDIA_URL=/<www path>/media/
    labels:
      traefik.enable: true
      traefik.http.routers.recipes.rule: Host(`<host>`) && PathPrefix(`/<sub path>`)
      traefik.http.routers.recipes.entrypoints: http
      traefik.http.services.recipes.loadbalancer.server.port: 8080
      traefik.http.services.recipes.loadbalancer.server.scheme: http

apache:

  Alias /<www path>/static/ /var/www/recipes/static/
  Alias /<www path>/media/ /var/www/recipes/media/
  <Directory "/var/www/recipes/">
    Require all granted
  </Directory>

I used two paths <sub path> and <www path> for simplicity. In my case I have <sub path> = recipes and <www path> = serve/recipes. One could also change the matching rules of traefik to have everything under one path.

I left out the TLS config in this example for simplicty.

@incaseoftrouble incaseoftrouble added the enhancement New feature or request label Dec 18, 2020
@vabene1111
Copy link
Collaborator

thank you! We are getting to a point where we have quite a few "unoffical" setup instructions. Maybe i will create a section in the discussions area or maybe i will add this to the setup examples.

@incaseoftrouble
Copy link
Author

If you want I could create a fully functional configuration (i.e. something that cd docs/docker/traefik-subpath-apache && docker-compose up -d yields a functional instance)

@vabene1111
Copy link
Collaborator

hmm you mean by cloning the whole repo first ?

this is not really in the sense of the installation process that is common at this point. But that said the installation process isn't great.

I guess for now the instructions above suffice. At some point the installation process will be revisited and we will think about this as well.

@vabene1111
Copy link
Collaborator

i have added your example to the new docs. Feel free to open a PR (or issue) if you want anything changed or want to improve it further

@jscmidt
Copy link

jscmidt commented Dec 8, 2021

Is it really necessary to expose the webserver-service (Nginx/Apache) and the recipes-service with Traefik?
The default example here only exposes the webserver-service but not the recipes-service if I got it right.

@incaseoftrouble
Copy link
Author

IIRC it is not necessary, but I let apache serve the static data.

@jscmidt
Copy link

jscmidt commented Dec 10, 2021

Hi,
I just don’t get it to work. Tandoor is running behind /recipes/ as expected, but unfortunately there are some requests to /api/ instead of /recipes/api/, which of course doesn’t.

The relevant parts in my .env:

SCRIPT_NAME=/recipes
STATIC_URL=/recipes/static/
MEDIA_URL=/recipes/media/

The traefik labels of the nginx-container:

- "traefik.enable=true"
- "traefik.http.routers.recipes-local.entrypoints=web"
- "traefik.http.routers.recipes-local.rule=Host(`IP`)&&PathPrefix(`/recipes/`)"

Nginx-config:

server {
  listen 80;
  server_name localhost;

  client_max_body_size 128M;

  location /recipes/media/ {
    alias /media/;
  }
  location /recipes/static/ {
    alias /static/;
  }
  # pass requests for dynamic content to gunicorn
  location / {
    proxy_set_header Host $http_host;
    proxy_pass http://web_recipes:8080;
  }
}

@smilerz
Copy link
Collaborator

smilerz commented Dec 10, 2021

@JSAnyone you also need to set the lines below in your nginx config

    proxy_set_header X-Script-Name /recipes;
    proxy_cookie_path / /recipes;

(I'm actually not 100% sure about the proxy_cookie_path, but I'm assuming I put it there for a reason?)

@jscmidt
Copy link

jscmidt commented Dec 10, 2021

That‘s it, Thanks!
Maybe it‘s helpful for others to add this to the documentation as a nginx-example?

.env:

SCRIPT_NAME=/recipes
STATIC_URL=/recipes/static/
MEDIA_URL=/recipes/media/

Nginx-config (/var/lib/docker/volumes/yourvolumename/_data/Recipes.conf):

server {
  listen 80;
  server_name localhost;

  client_max_body_size 128M;

  location /recipes/media/ {
    alias /media/;
  }
  location /recipes/static/ {
    alias /static/;
  }

  # pass requests for dynamic content to gunicorn
  location / {
    proxy_set_header Host $http_host;
    proxy_pass http://web_recipes:8080;
    proxy_set_header X-Script-Name /recipes;
    proxy_cookie_path / /recipes;
  }
}

docker-compose.yml:

version: "3"
services:
  db_recipes:
    restart: always
    image: postgres:11-alpine
    volumes:
      - postgres-storage:/var/lib/postgresql/data
    env_file:
      - ./.env

  web_recipes:
    image: vabene1111/recipes
    restart: always
    env_file:
      - ./.env
    volumes:
      - staticfiles-storage:/opt/recipes/staticfiles
      - nginxconfig-storage:/opt/recipes/nginx/conf.d
      - mediafiles-storage:/opt/recipes/mediafiles
    depends_on:
      - db_recipes

  nginx_recipes:
    image: nginx:mainline-alpine
    restart: always
    env_file:
      - ./.env
    depends_on:
      - web_recipes
    volumes:
      - nginxconfig-storage:/etc/nginx/conf.d:ro
      - staticfiles-storage:/static
      - mediafiles-storage:/media
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.recipes-local.entrypoints=web"
      - "traefik.http.routers.recipes-local.rule=Host(`IP`)&&PathPrefix(`/recipes/`)"

volumes:
  postgres-storage:
  nginxconfig-storage:
  staticfiles-storage:
  mediafiles-storage:

@jscmidt
Copy link

jscmidt commented Dec 10, 2021

Well, it have a new problem: Since I logged out I can't log in again. Tandoor says that I am logged in successfully, but I'm not really logged in.
Could be something Tandoor-general, but I think it has to do with the subpath... Any ideas?

@smilerz
Copy link
Collaborator

smilerz commented Dec 10, 2021

@JSAnyone can you clear any cache that exists (or go to private mode) and see if the problem persists?

@jscmidt
Copy link

jscmidt commented Dec 10, 2021

Thanks again, I could have thought of that myself. After deleting the cache logging in worked.

Something else: When clicking on the "Tandoor" logo I get redirected to http://IP/ instead of http://IP/recipes/. Is there a way I can change this behavior?
image

@smilerz
Copy link
Collaborator

smilerz commented Dec 10, 2021

that's weird - I don't even see that.
image

I'm going to assume that's a bug - can you open a new issue and someone will fix that?
@Kaibu - is that Tandoor button only available in the Tandoor theme?

@jscmidt
Copy link

jscmidt commented Dec 10, 2021

I tried it out, the button is only visible in the Tandoor-Theme.
I will open a new issue later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants