#Static Files deploying in prod Mode Issue (Daphne- apache- behind a proxy) unknown issue ?

38 messages · Page 1 of 1 (latest)

finite rampart
#

i'am facing a vicious issue accessing static files correctly deployed with collectstatic-
the files are normally viewed in dev mode - but in prod mode noway!
here is my conf:
-Python 3.9 (for specific requirements packages)
-Django 3.2.9

  • django app located in VBOX VM /HOME/PycharmProjects/djangoapp (VM is hosted & bridged on a physical ubuntu server)
  • I use daphne 3.0.2 for asgi /home/PycharmProjects/djangoapp/venv39/bin/daphne -b 0.0.0.0 -p 9099 djangoapp.asgi:application
  • i use apache :
    -> to serve static files in the VM
    -> as a reverse proxy to root requests from outside to the VM
    My settings.py
    ...
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(file)))

STATIC_URL = '/static/'
STATIC_ROOT = '/var/www/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = '/var/www/media/'
..
DEBUG = False

......................
Appache (IN VM):

Alias /static/ /var/www/static/
<Directory /var/www/static/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

# Fichiers médias
Alias /media/ /var/www/media/
<Directory /var/www/media/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# Autres configurations pour l'application (si besoin)
ProxyPass / http://127.0.0.1:9099/
ProxyPassReverse / http://127.0.0.1:9099/

</VirtualHost>

i grant permission to VM_apache to acces static /media folders with : sudo chown -R www-data:www-data /var/www/static /var/www/media

in local dev mode all things are ok- but passing to Prod - the app is going on except static files (404 for all the files)
Config Issue? Permission? Ubuntu ?

icy ether
#

a 404 for static files in prod is very common

#

I'd guess your apache config is wrong, but I don't know apache well enough to diagnose the problem.

#

You should be able to debug apache without django at all, simply by using "curl" to try to fetch the static files, and examining apache's log files.

finite rampart
#

thanks @icy ether for your reply- actually i use apach for 2 reasons - in the hoste as reverse proxy to manage ssl and routes to the diffrents VM-
inside Django VM to serve the static files - Daphne beeing the asgi-
the VM is bridged ine the VBox network - so it has its proper ip- all congs seems to be ok as the documentation says- but noway to sxolve the problem- i used curl and its the same -

icy ether
#

do you mean "I used curl and it, too, is getting a 404"?

#

If so, that's good

#

it means the problem has nothing to do with django

neat mirage
#

Can you ssh into the server and run ls -lh /var/www/static/?

icy ether
#

you might want to see if the "ProxyPass" settings "overrride" the "Directory" sections

#

in fact, if I were you, I'd stop django, then try "curl" again. If you get a 500 error, that strongly suggests that apache is ignoring the "Directory" sections, and instead proxying everything to Django. Which would be bad; you only want it to proxy the non-static paths to Django.

finite rampart
#

but in the browser iget 404

neat mirage
#

How are you running Django? never mind, I see it

finite rampart
#

@neat mirage here is a sample of ls : drwxr-xr-x 2 www-data www-data 4,0K déc. 5 13:58 account
drwxr-xr-x 6 www-data www-data 4,0K déc. 5 13:58 admin
drwxr-xr-x 5 www-data www-data 4,0K déc. 5 13:58 admin_interface
drwxr-xr-x 5 www-data www-data 4,0K déc. 5 13:58 ajax_datatable
drwxr-xr-x 2 www-data www-data 4,0K déc. 5 13:58 autocomplete_light

icy ether
#

is daphne running in debug mode, or production?

neat mirage
#

What is the request URL in the browser's network tab of the CSS file? (You can just share the relative path, e.g. /static/style.css if you want to mask your domain).

finite rampart
neat mirage
finite rampart
neat mirage
icy ether
#

well I didn't mean to run curl against the django endpoint; obviously that won't work. I meant to run it against apache, to see if it's proxying the static files.

finite rampart
finite rampart
neat mirage
#

You have two separate apache instances in the VM?

finite rampart
#

@neat mirage no not in the same VM- one in VM and one in hosting machine -in the host igot other website

#

@neat mirage schema is static file request>HOST>Apach1>virtualhost1>apach_VM

neat mirage
#

Can you share your complete Apache config in the VM?

finite rampart
# neat mirage Can you share your complete Apache config in the VM?

ok -
<VirtualHost *:80>
ServerName localhost

DocumentRoot /var/www/html

# Fichiers statiques
Alias /static/ /var/www/static/
<Directory /var/www/static/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# Fichiers médias
Alias /media/ /var/www/media/
<Directory /var/www/media/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

# this intercepts apache host requests and redirect to daphne
ProxyPass / http://127.0.0.1:9099/
ProxyPassReverse / http://127.0.0.1:9099/

</VirtualHost>

neat mirage
#

Hmmm.. very confusing.
It should not be working with curl http://192.168.1.77:9099/static/style.css, as that should only hit the Daphne instance, and this should not be serving static files at all 🤔

#

Is 192.168.1.77 the VM IP?
Does curl http://192.168.1.77/static/style.css work?

finite rampart
#

how to add a screenshot !

finite rampart
neat mirage
#

Comment out the proxy settings, and check again?

finite rampart
#

done before no changes

neat mirage
#

Hmmm... does adding quotes change anything?

<VirtualHost *:80>
    ServerName localhost

   # DocumentRoot /var/www/html

    # Fichiers statiques
    Alias "/static/" "/var/www/static/"
    <Directory "/var/www/static/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    # Fichiers médias
    Alias "/media/" "/var/www/media/"
    <Directory "/var/www/media/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>

    # this intercepts apache host requests and redirect to daphne
    ProxyPass "/" "http://127.0.0.1:9099/"
    ProxyPassReverse "/" "http://127.0.0.1:9099/"
</VirtualHost>