简体   繁体   中英

django/apache2 serving media isn't working on production

i'm running django mod_wsgi with apache2 on GCP VM instance and when i run it on DEBUG=False the static files work fine but media files is geting 404 when i check them while clearly the files are present on the server . my conf is based on the following django doc https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/modwsgi/#serving-files

#settings.py
MEDIA_ROOT = os.path.join(PROJECT_ROOT, "media")
MEDIA_URL = os.environ.get("MEDIA_URL", "/media/")

STATIC_ROOT = os.path.join(PROJECT_ROOT, "static")
STATIC_URL = os.environ.get("STATIC_URL", "/static/")

#apache2
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

<Directory /home/ubuntu/myshop/static>
Require all granted
</Directory>

tried restarting apache2 and even the server but didnt work either

Looks like your alias is wrong...

#apache2
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

the alias has a backslash and your directory statement does not.

fixed it was an https problem i needed to add the config to both virtualhost file port 80 and 443

<VirtualHost *:80>
...
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

<Directory /home/ubuntu/myshop/static>
Require all granted
</Directory>
...
</VirtualHost>
<VirtualHost *:443>
...
Alias /media/ /home/ubuntu/myshop/media/
Alias /static/ /home/ubuntu/myshop/static/

<Directory /home/ubuntu/myshop/media>
Require all granted
</Directory>

<Directory /home/ubuntu/myshop/static>
Require all granted
</Directory>
...
</VirtualHost>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM