My Django project deployed on EC2 instance. Everything is fine except static files. Here is my project directory path tree
my_project
└─admin
├── admin
│ ├── admin
│ ├── keywords
│ ├── manage.py
│ └── static
├── README.md
And this is my settings.py setting code about staticfiles
# settings.py
debug=False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = os.path.dirname(BASE_DIR)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'my_project/static')
STATICFILES_DIRS = [
STATIC_ROOT
]
And this is my nginx.conf files
root /usr/share/nginx/html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /static/;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/devel/run/gunicorn.sock;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
And also my machine's environments:
Centos 7.9
python 3.6.8
Django 4.0.6
I already did python manage.py collectstatic command.
When I enter into this project's Admin page, Every UI have loaded without CSS and JS though.
Is somewhere of my setting is wrong? I wonder why django cannot find any static files.
Assuming your BASE_DIR is the same as /usr/share/nginx/html, and the BASE_DIR/myproject/static exists and has content, try
location /static/ {
root /myproject/;
}
(Using root rather than alias as the end directories are the same, as per nginx docs)
I am assuming your earlier root definition will prefix the location - if not you might have to use root /usr/share/nginx/html/myproject/ explicitly instead
Related
I'm trying to deploy a django app in a EC2 instance but I'm currently having issues serving the static files of the app.
Currently, my nginx's conf file looks like this:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://0.0.0.0:8000;
}
location /static/ {
#autoindex on;
root /home/ubuntu/some/folder/static/;
}
}
Also in my settings.py file in my django project I have the following config related to my static files:
STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/some/folder/static/'
and Gunicorn, I'm launching it as the following:
gunicorn3 --bind 0.0.0.0:8000 myproject.wsgi:application
And with that, if I access http://ec2-my-instanceI see the html content, but all my js and css files get error 404.
I'm kinda lost and I don't know what I'm missing to make it works, I already migrated the static content and I checked the folder which nginx is looking for the static files have them, but if I check the browser's console I see this kind of errors:
GET http://ec2-my-instance/static/somefile.js net::ERR_ABORTED 404 (Not Found)
You want 'alias' not 'root':
location /static/ {
alias /home/ubuntu/some/folder/static/;
}
I haveNginx + Gunicorn + Django server deployed on Ubuntu. It running with Debug=False settings and everything works fine. CSS is loaded fine, JS works as well. But when I try to update the css file or js file, the changes I made are not reflected when I run the server.
I tried to update an old static files, also static file created after collectstatic command, I also tried clean collectstatic as well as systemctl restart nginx (and gunicorn). I also cleaned my cache in browser. But when I look a page source code, these changes are not there.
this is how my nginx config looks like
server {
listen 80;
server_name mediadbin.n-media.co.jp;
client_max_body_size 500M;
access_log /home/mediaroot/mediadbin/logs/nginx-access.log;
error_log /home/mediaroot/mediadbin/logs/nginx-error.log;
server_tokens off;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /static { $ I TRIED APPLY CHANGES HERE <- NOTHING happens
alias /home/mediaroot/mediadbin/mediadbin/static;
}
location /media {
alias /home/mediaroot/mediadbin/mediadbin/media;
}
include global404;
}
HERE is my gunicorn config
#!/bin/bash
# Name of the application
NAME="mediadbin"
# Django project directory
DJANGODIR=/home/mediaroot/mediadbin/mediadbin
# how many worker processes should Gunicorn spawn
NUM_WORKERS= $(( $(nproc) * 2 + 1 ))
# which settings file should Django use
DJANGO_SETTINGS_MODULE=mediadbin.settings
# WSGI module name
DJANGO_WSGI_MODULE=mediadbin.wsgi
echo "Starting $NAME as `whoami`"
# Activate the virtual environment
cd $DJANGODIR
ENV=new_django
source /root/.virtualenvs/new_django/bin/activate
# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--timeout 600
--name $NAME \
--workers &NUM_WORKERS \
--bind=127.0.0.1 \
--log-level=debug \
--log-file=-
>>> sudo find / -name menu_detail_look.js
/home/mediaroot/mediadbin/mediadbin/static/main_app/js/menu_detail_look.js
/home/mediaroot/mediadbin/mediadbin/main_app/static/main_app/js/menu_detail_look.js
↑ Updated both, nothing happens (no err in js also)
First of all, if you set static location nginx then static files will definitely be loaded through nginx.
make you static block like (instead of alias use root):
location /static/ {
root /home/mediaroot/mediadbin/mediadbin/static/;
}
Also make sure static root is set to: /home/mediaroot/mediadbin/mediadbin/static/
Running collectstatic will copy all static files to static root folder and nginx will serve it.
Hmm it's been so long , Aliright if now anyone working on this can check this solution
in urls.py
from django.conf import settings
from django.conf.urls.static import static
then at bottom define
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
OR (DEPEND on your env.)
if settings.DEBUG:
# static files (images, css, javascript, etc.)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and in settings.py
define media path
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
MEDIA_URL = "/media/"
Make sure to define path in /etc/nginx/sites-available/domainxxx
If your folder path is /home/admin/django/project/media
location /media/ {
root /home/admin/django/project;
}
I'm a newbie in Django and I have this problem.
When I enter to my admin dashboard, CSS and JS files aren't shown. When I follow the URL, it appears
404 Not Found
I'm using Nginx + Gunicorn.
This is my Nginx configuration:
server {
server_name MYPROJECT.COM;
access_log off;
location /static {
alias /opt/myenv/myenv/MYPROJECT/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
Also this is settings.py static configuration:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
My css admin files are stored in/opt/myenv/myenv/MYPROJECT/static/admin
, but also in /opt/myenv/myenv/static/admin
I've uploaded the files in both directories and run collectstatic
Can anyone help me?
Thanks, everyone!
We had issues getting collectstatic working with admin in an older version of Django, and fixed it in our nginx config by doing something like
location /static {
root /path/project;
}
location /static/admin {
root /path/venv/lib/python3.4/site-packages/django/contrib/admin;
}
I have a Django/Django-Rest-Framework project, and it only provides the APIs.
This is one of the drf APIs, I request the remote ipaddress in the browser:
I can not request its static resources.
I am not sure whether when Django APIs distribute, I will config the nginx for static and media placement.
If need Nginx config for Django APIs static resources, How can I config it?
this is an example of nginx vhost configure file:
server {
listen 80;
server_name www.abc.xyz;
access_log logs/www.abc.access.log main;
location / {
root /var/www/html/website/;
index index.html index.htm;
}
location ~ /media/*\.(jpg|png|jpeg|bmp|gif|swf)$
{
access_log off;
expires 30d;
root /var/www/html/python_backend/abc;
break;
}
location /api/ {
proxy_pass http://101.20.32.76:8000/api/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /media/ {
proxy_pass http://101.20.32.76:8000/media/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
You see the localtion /:
location / {
root /var/www/html/website/;
index index.html index.htm;
}
because my Django/Django-Rest-Framework is only provide APIs, there is no such index.html files, so I don't know how to config it.
and the http://101.20.32.76:8000/media/ I can not access, so the configuration of location /media/ and location /static/ is invalid right?
EDIT-1
I deploy in my remote server (CentOS7.2)
and in my remote repo, there are static directory and media directory, because I pushed them from local repo, they are not in .gitignore.
EDIT-2
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this is the configuration of my settings.py.
During your application deployment; you'll need to run python manage.py collectstatic
https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/#collectstatic
Collects the static files into STATIC_ROOT.
Files are searched by using the enabled finders. The default is to
look in all locations defined in STATICFILES_DIRS and in the 'static'
directory of apps specified by the INSTALLED_APPS
Read this for more info on how and when to collect static and how to serve static assets with Django:
https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-the-site-and-your-static-files-from-the-same-server
Im trying to collect static files on django production hosting
website: jumentosemuaresonline.com.br
My configuration:
DEBUG = False
ALLOWED_HOSTS = [".jumentosemuaresonline.com.br"]
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
STATIC_URL = os.path.join(BASE_DIR,'/static/')
When i print static root on setting, returns the exact path of the ftp
/home/jumentosemuaresonline/apps_wsgi/website/static/
when i execute python manage.py collectstatic -v 0
It also return the exact path of the ftp
/home/jumentosemuaresonline/apps_wsgi/website/static/
But when i execute python mange.py collectstatic it dont fill the static folder with the static files, only returns
0 static files copied, 109 unmodified.
Anyone have idea what am i missing?
I figured out what was happening:
The collect static command was working fine but I was using Nginx in front of the application as a reverse proxy, Nginx has to serve the static path not django.
upstream django_app {
server djangohost:8000;
}
server {
listen 80;
location / {
proxy_pass http://django_app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /my/static_path/;
}
}
It says 109 unmodified. The files are already there.
Try ls /home/jumentosemuaresonline/apps_wsgi/website/static/
That should show you all the files there.
Just in case. Try this too.
rm -rf /home/jumentosemuaresonline/apps_wsgi/website/static/
mkdir /home/jumentosemuaresonline/apps_wsgi/website/static/
python manage.py collectstatic
This should show you 109 copied and 0 modified.