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

Error handling request to static files #1878

Closed
danieldietsch opened this issue Jun 24, 2022 · 11 comments
Closed

Error handling request to static files #1878

danieldietsch opened this issue Jun 24, 2022 · 11 comments
Labels
setup issue possibly or definitely an issue with the user setup

Comments

@danieldietsch
Copy link

danieldietsch commented Jun 24, 2022

Issue

I am just setting up Tandoor with docker compose and an external reverse proxy in a subfolder.
So far, I can access https://domain/recipes/setup/, but all requests to static assets fail with the following error.

tandoor-nginx_recipes-1  | 192.168.80.1 - <user> [24/Jun/2022:21:12:41 +0000] "GET /static/assets/favicon-16x16.fe125d94b939.png HTTP/1.1" 500 141 "https://domain/recipes/setup/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36 OPR/87.0.4390.45" "clientip"
tandoor-web_recipes-1    | [2022-06-24 23:12:41 +0200] [13] [ERROR] Error handling request /static/assets/favicon-16x16.fe125d94b939.png
tandoor-web_recipes-1    | Traceback (most recent call last):
tandoor-web_recipes-1    |   File "/opt/recipes/venv/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 136, in handle
tandoor-web_recipes-1    |     self.handle_request(listener, req, client, addr)
tandoor-web_recipes-1    |   File "/opt/recipes/venv/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 169, in handle_request
tandoor-web_recipes-1    |     resp, environ = wsgi.create(req, client, addr,
tandoor-web_recipes-1    |   File "/opt/recipes/venv/lib/python3.10/site-packages/gunicorn/http/wsgi.py", line 183, in create
tandoor-web_recipes-1    |     path_info = path_info.split(script_name, 1)[1]
tandoor-web_recipes-1    | IndexError: list index out of range

It seems I am missing the right combination of the various URLs in the .env file. I have

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

Without setting STATIC_URL and MEDIA_URL, the requests get rewritten to https://domain/static instead of https://domain/recipes/static, and hence rejected by Apache.

Do you know how this could be fixed?

Tandoor Version

latest

OS Version

Gentoo

Setup

Docker / Docker-Compose

Reverse Proxy

Others (please state below)

Other

Apache 2.4.54

Environment file

DEBUG=0
SQL_DEBUG=0
HTTP_PORT=127.0.0.1:8281
ALLOWED_HOSTS=*
TIMEZONE=Europe/Berlin
DB_ENGINE=django.db.backends.postgresql
POSTGRES_HOST=db_recipes
POSTGRES_PORT=5432
POSTGRES_USER=djangouser
POSTGRES_DB=djangodb
FRACTION_PREF_DEFAULT=0
COMMENT_PREF_DEFAULT=1
SHOPPING_MIN_AUTOSYNC_INTERVAL=5
SCRIPT_NAME=/recipes
STATIC_URL=recipes/static/
MEDIA_URL=recipes/media/
GUNICORN_MEDIA=0
REVERSE_PROXY_AUTH=0
ENABLE_PDF_EXPORT=1

Docker-Compose file

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

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

  nginx_recipes:
    image: nginx:mainline-alpine
    restart: always
    ports:
      - ${HTTP_PORT}:80
    env_file:
      - ./.env
    depends_on:
      - web_recipes
    volumes:
      - nginx_config:/etc/nginx/conf.d:ro
      - staticfiles:/static:ro
      - mediafiles:/media:ro

volumes:
  nginx_config:
    driver: local
    driver_opts:
      type: bind
      o: bind
      device: $PWD/nginx_config
  mediafiles:
    driver: local
    driver_opts:
      type: bind
      o: bind
      device: $PWD/mediafiles
  staticfiles:
  db:

Relevant logs

see above

@danieldietsch danieldietsch added the setup issue possibly or definitely an issue with the user setup label Jun 24, 2022
@stewartadam
Copy link
Contributor

stewartadam commented Sep 22, 2022

I'm seeing this as well using the "plain" docker-compose setup. Setting SCRIPT_NAME=/recipes (or FORCE_SCRIPT_NAME) results in static and media files being served at the expected URL of /recipes/static/..., but the calls to %{ script %} in views appear to ignore SCRIPT_NAME when generating the HTML views so the paths are all mismatched - they browser gets sent paths as /static/... so you get 404s.

According to https://code.djangoproject.com/ticket/25598 when STATIC_URL and MEDIA_URL are relative SCRIPT_NAME is supposed to be automatically injected, which doesn't appear to be happening but I'm not sure why.

edit: I added some debugging statements to the Django code and it looks like it does see the SCRIPT_NAME prefix when serving a request, but the app initializes with an empty SCRIPT_NAME value and that gets cached in Django application config, so subsequent calls to static in the HTML templates simply return the / base path and not the sub-path as defined by SCRIPT_NAME for that request.

@stewartadam
Copy link
Contributor

I think Django broke this functionality since adding it in 2019 because application settings appear to be cached in a dict now.
Created https://code.djangoproject.com/ticket/34028.

@vabene1111
Copy link
Collaborator

ok thanks for reporting!

@stewartadam
Copy link
Contributor

After a bunch of debugging, this appears to be related to whitenoise middleware.

Django's will inspect SCRIPT_NAME header or FORCE_SCRIPT_NAME setting to set the internal prefix, which is then applied to STATIC_URL and MEDIA_URL -- however, these settings all get cached based on the first access to these settings.

Normally that would be the very first HTTP request to the Django server and all would be well; but whitenoise attempts to access STATIC_URL during initialization; of course being outside a HTTP request context, there's no SCRIPT_NAME header to read so the prefix is just "/" and gets cached that way.

That's why the pathing is correct but all the static URLs are wrong.

@ryneeverett
Copy link

Has anyone found a workaround?

@danieldietsch
Copy link
Author

danieldietsch commented Jan 22, 2023

Thank you for looking into that.
I never came around to try again until today, but I can say the behavior is still the same (with the current docker image at least).

However, following https://docs.tandoor.dev/install/other/ and in particular, reading issue #266 with its modifications to the Recipes.conf, I managed to get most of the static files
The only thing I see that is not resolved to /<subpath>/static is poppins_latin_*.woff2 ; strangely enough, webfonts/fa-solid-900.44d537ab79f9.woff2 is working as expected (as do all .css, .js,.png, .svg requests).

Sadly, this does not really help, because now I can create a user, login, and logout, but nothing else. I am somehow stuck on the "Space" page.
Edit: It suddently started working, perhaps some caching thing (not the poppings webfont though).

@aulemahal
Copy link

@danieldietsch I think I had the same static file problem and the same solution. And I too have only the webfonts issue remaining.

Looking at the code, it seems the webfonts' urls are hardcoded in the theme. For example here :

src: url(/static/webfonts/poppins_devanagari_400.woff2) format('woff2');

It seems these paths are not modified by the settings in the env file.

@ncovercash
Copy link

@danieldietsch any chance you'd be willing to share your docker/nginx configuration? I've been trying at this a while and haven't been able to make anything work :(

@smilerz
Copy link
Collaborator

smilerz commented Dec 12, 2023

I've been trying at this a while and haven't been able to make anything work :(

Please open a new issue and share your configuration and logs.

@danieldietsch
Copy link
Author

@ncovercash I did not pursue this any further. I didn't get the fonts working, and it was unusable for me.

@smilerz
Copy link
Collaborator

smilerz commented Mar 8, 2024

as a workaround for subfolder you have to serve static content with nginx or some other web server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
setup issue possibly or definitely an issue with the user setup
Projects
None yet
Development

No branches or pull requests

7 participants