nginx not serving django static files - python

I'm running my django project on amazon AMI machine and have problem with nginx serving static files. Project static folder path is /home/user/projectname/app/static and nginx.conf is
server {
listen 80;
location /static {
alias /home/user/projectname/app/static;
}
location / {
proxy_pass http://localhost:8000;
}
I tried to make collectstatic and change static location to alias /home/user/static; but it didn't help. What am i doing wrong?

Make sure that you chown the home directory. This makes the static files accessible to nginx.
chown -R user:user /home
chmod -R ug+r /home
Also, try running python manage.py collectstatic --noinput
Hope this solves the problem.

please change the static directory into staticstorage
server {
listen 80;
location /static {
alias /home/user/projectname/app/staticstorage;
}
location / {
proxy_pass http://localhost:8000;
}

Related

Django + nginx + gunicorn not being able to serve static files

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/;
}

Nginx wont update static files for Django

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;
}

Django doesn't serve static files with NGINX + GUNICORN

Everything worked very well before gunicorn and nginx, static files were served to the website.
But now, it doesn't work anymore.
Settings.py
STATICFILES_DIRS = [
'/root/vcrm/vcrm1/static/'
]
STATIC_ROOT = os.path.join(BASE_DIR, 'vcrm/static')
STATIC_URL = '/static/'
MEDIA_ROOT = '/root/vcrm/vcrm1/vcrm/media/'
MEDIA_URL = '/media/'
/etc/nginx/sites-available/vcrm
server {
listen 80;
server_name 195.110.58.168;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
root /root/vcrm/vcrm1/vcrm;
}
location = /media {
root /root/vcrm/vcrm1/vcrm;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
When I run collectstatic:
You have requested to collect static files at the destination
location as specified in your settings:
/root/vcrm/vcrm1/vcrm/static
This will overwrite existing files!
Are you sure you want to do this?
and then:
Found another file with the destination path 'admin/js/vendor/jquery/jquery.min.js'. It will be
ignored since only the first encountered file is collected. If this is not what you want, make sure
every static file has a unique path.
0 static files copied to '/root/vcrm/vcrm1/vcrm/static', 251 unmodified.
NGINX + Gunicorn + Django
Django project:
djangoapp
- ...
- database
- djangoapp
- settings.py
- urls.py
- ...
- media
- static
- manage.py
- requirements.txt
Server: install venv, requirements.txt:
sudo apt-get update
sudo apt-get install -y git python3-dev python3-venv python3-pip supervisor nginx vim libpq-dev
--> cd djangoapp
pathon3 -m venv venv
source venv/bin/activate
(venv) pip3 install -r requirements.txt
Server: install NGINX:
sudo apt-get install nginx
sudo vim /etc/nginx/sites-enabled/default
Server: NGINX config:
server {
listen 80 default_server;
listen [::]:80 default_server;
location /static/ {
alias /home/ubuntu/djangoapp/static/;
}
location /media/ {
alias /home/ubuntu/djangoapp/media/;
}
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;
proxy_redirect off;
add_header P3P 'CP="ALL DSP COR PSAa OUR NOR ONL UNI COM NAV"';
add_header Access-Control-Allow-Origin *;
}
}
Server: setup supervisor:
cd /etc/supervisor/conf.d/
sudo vim djangoapp.conf
Server: supervisor config:
[program:djangoapp]
command = /home/ubuntu/djangoapp/venv/bin/gunicorn djangoapp.wsgi -b 127.0.0.1:8000 -w 4 --timeout 90
autostart=true
autorestart=true
directory=/home/ubuntu/djangoapp
stderr_logfile=/var/log/game_muster.err.log
stdout_logfile=/var/log/game_muster.out.log
Server: update supervisor with the new process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl restart djangoapp
Run python manage.py collectstatic from your project directory.
Access nginx's webserver config file and add a location in your config file.
Reload nginx server with sudo systemctl reload nginx.

why nginx not starting my service?

I am using linux fedora, i installed nginx as sudo yum install nginx.I have created a django project and assigned a port as guicorn projectname.wsgi:application --bind=127.0.0.1:8001.And i created a file in /etc/nginx/sites-enabled/default
in my default file i have the following code:
server {
listen localhost:8006;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
autoindex on;
alias /home/user/Desktop/projects/28-05-2014/HMS/static/;
}
}
When i checked my nginx server home as localhost:80 it is running.But when i called localhost:8006 it is not connecting. When i checked active connections with netstat -lnt | grep 80 i found only nginx default service is running.What mistake i am doing.Any help would be appriciated
Place the above code in nginx.conf file which is located in /etc/nginx/.Then your configuration file looks like this
nginx.conf
server {
listen localhost:8006;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /static/ {
autoindex on;
alias /home/user/Desktop/projects/28-05-2014/HMS/static/;
}
}
Not enough information to answer.
Enable logging:
access_log /var/log/nginx/example.access.log;
error_log /var/log/nginx/example.error.log;
And look at what the log files says. If you cannot figure it out, please post the log outputs here. Question does not provide enough data. I ll update this answer as you update your question.

Django not collecting static files

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.

Categories

Resources