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

Django Static files serving #1718

Closed
Maxino22 opened this issue Jan 2, 2022 · 11 comments
Closed

Django Static files serving #1718

Maxino22 opened this issue Jan 2, 2022 · 11 comments
Labels

Comments

@Maxino22
Copy link

Maxino22 commented Jan 2, 2022

Hello am new to nginx proxy manger and soo far i have understood the workflow however in the location section am having a bit of trouble.. am running a django app with gunicorn which only handles python code hence my static files(javaScript & CSS) are not being served..
i want to include them in the location area but every time I do it the host goes offline... if I used an nginx config file traditionally this is what it would look like

server {
    listen 80;
    server_name YOUR_IP_ADDRESS;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/djangoadmin/pyapps/btre_project;
    }
    
    location /media/ {
        root /home/djangoadmin/pyapps/btre_project;    
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

I want the proxy params , media and static to be included in location but I can't find a way... kindly assist

@chaptergy
Copy link
Collaborator

The way I understand you are trying to do it is not possible. I'm assuming you run the django app with gunicorn in a container? Then npm has no access to the files you are trying to serve. Furthermore static file serving is not supported officially by npm, there only is a feature request for it. You should theoretically be able to have static file serving by mounting the files into the container and using the advanced config, but we can't officially support this, you might want to try the reddit community.
But as a tip: only the three non-/ locations are relevant, the / location and stuff above the locations are automatically generated. You also want to use the advanced config, as the custom locations generate code for proxying, not for file serving. But pasting the three locations into the advanced tab and mounting the files into the npm container should work.

@Maxino22
Copy link
Author

Maxino22 commented Jan 3, 2022

Thank you very much for replying . I honestly did not understand but I found a work around using whitehorse to serve my statics directly from the python. I tried looking at the credit community for answers but none of them were to detail. finally I would like some assist on redirecting as the HTTP versions of my domain are still accessible and the www does not redirect to non www .... i saw redirect host but documentation and online searches don't quite show

@chaptergy
Copy link
Collaborator

Create a redirect host for www.example.com and redirect it to example.com usually with a 301 redirect. If you know, that this redirect will change in the future, use a 302 redirect to indicate it is temporary.
To make sure you only use HTTPS just enable Force SSL and HSTS in your proxy host.

@Maxino22
Copy link
Author

Maxino22 commented Jan 3, 2022

Wow thank you that actually worked

@keniobats
Copy link

@Maxino22 Hi! any updates on this? I'm facing the same issue. Couldn't find any references to "whitehorse" you mention. Thanks!

@Maxino22
Copy link
Author

sorry mate its not whitehorse rather white noise... it makes running django quite easy.... http://whitenoise.evans.io/en/stable/ ... it will be easy read the docs i beleive will solve problem

@keniobats
Copy link

Thanks @Maxino22 I'll give it a shot!

@keniobats
Copy link

I've already solved with Whitenoise, thanks @Maxino22 !!!

@Maxino22
Copy link
Author

Maxino22 commented Oct 2, 2022

you welcome... Glad i could help 🔥

@snake-soft
Copy link

It can cause problems when adding an advanced configuration and enable "cache assets".
I got a 404 trying to load asset files (Django static and media).
It works after disabling "cache assets".

@mr-engin3er
Copy link

It can cause problems when adding an advanced configuration and enable "cache assets". I got a 404 trying to load asset files (Django static and media). It works after disabling "cache assets".

This solved my issue.

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

No branches or pull requests

5 participants