Reputation: 3012
I have set up a django server, using this guide:
https://gist.github.com/evildmp/3094281
The uwsgi is serving correctly, but the static files are obviously missing as they are the responsibility of nginx. So it seems nginx is my issue. My nginx config is the same as what is on that guide.
But here is my base.py settings file:
########## PATH CONFIGURATION
# Absolute filesystem path to the Django project directory:
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
# Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT)
BASE_DIR = dirname(dirname(abspath(__file__)))
# Site name:
SITE_NAME = "sasite"
# Add our project to our pythonpath, this way we don't need to type our project
# name in our dotted import paths:
path.append(DJANGO_ROOT)
########## END PATH CONFIGURATION
########## MEDIA CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'
########## END MEDIA CONFIGURATION
########## UPLOAD CONFIGURATION
UPLOAD_ROOT = join(BASE_DIR, 'uploads')
UPLOADS_URL = '/uploads/'
FILE_UPLOAD_HANDLERS = (
"django.core.files.uploadhandler.MemoryFileUploadHandler",
"django.core.files.uploadhandler.TemporaryFileUploadHandler",
)
########## END UPLOAD CONFIGURATION
########## STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))
STATICFILE_ROOT = normpath(join(SITE_ROOT, 'static'))
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'
# See: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = (
normpath(join(SITE_ROOT, 'static')),
)
My config as I said is the same as in that guide I linked, the paths to the directories are correct, but I also have that assets directory which has some static files that support certain javascript plugins and such as I am using a theme. I assume adding another entry in my nginx.conf for assets would help with that, but nonetheless, I added a file to /media/ called 1.txt. When I go to the domainname.com:8001 (where uwsgi is serving) it comes up with no css, no javascript, etc. When I try to reach domainname.com:8000/media/1.txt (which nginx is serving) I get errors, such as empty response or it just does not work.
Since uWSGI is obviously serving the site, and no static files are being served, how can I fix nginx so that it is correctly serving the static files, and so that when I visit domainname.com:8000 it will show the full website with static files and all, since uWSGI is just supposed to be behind the nginx reverse-proxy.
I could use some help here, I understand how it all works but I just cant seem to get it working properly.
Thanks for any assistance you can provide.
EDIT:
nginx.conf
# nginx.conf
upstream django {
# connect to this socket
# server unix:///tmp/uwsgi.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket
}
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name .cshenkan.com; # substitute your machine's IP address or FQDN
charset utf-8;
#Max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /home/ubuntu/sasite-rewrite/media; # your Django pro$
}
location /static {
alias /home/ubuntu/sasite-rewrite/static; # your Django pro$
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # or the uwsgi_params you installe$
}
}
and the command I use to run uwsgi:
uwsgi --http :8001 --chdir /home/ubuntu/sasite-rewrite --wsgi-file /home/ubuntu/sasite-rewrite/sasite/wsgi.py
and the wsgi.py file:
from os.path import abspath, dirname
from sys import path
from django.core.wsgi import get_wsgi_application
SITE_ROOT = dirname(dirname(abspath(__file__)))
path.append(SITE_ROOT)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sasite.settings.production")
application = get_wsgi_application()
I have tried using unix sockets instead, changing the nginx config to use the unix sockets rather than web sockets, and by changing my uwsgi command to
uwsgi --socket /tmp/uwsgi.sock --chdir /home/ubuntu/sasite-rewrite --wsgi-file /home/ubuntu/sasite-rewrite/sasite/wsgi.py
But when I do that it does not work at all when I navigate to domainname.com:8000 and since I am no longer using the web socket for port 8001, I cannot test uwsgi alone by going to domainname.com:8001. Again it seems nginx is not working, and I'm not sure how to test uwsgi alone with unix sockets.
Any idea what I am doing wrong? I followed a few guides, and tried several methods, none seem to work, the closest I got is the gist I linked a the top. But when I tried to change to unix sockets (as I said) it seems to not work at all.
EDIT 2:
So now my new base.py settings file looks like this:
STATIC_ROOT = normpath(join(SITE_ROOT, 'static'))
STATIC_URL = '/static/'
STATICFILES_DIRS = (
normpath(join(SITE_ROOT, 'static')),
normpath(join(SITE_ROOT, 'assets')),
)
Will this configuration work with the way I have it set up? I have static/ as my typical django static directory, but assets/ is for the theme I am using and I need static files served from there as well. If I add it to STATICFILES_DIRS will that do the trick? Do I need to add anynthing to nginx.conf? For example craete something for assets in the nginx.conf the same way I set up static?
e.g. add this to nginx.conf:
location /assets {
alias /home/ubuntu/sasite-rewrite/assets
}
Is this the correct approach? Unfortunately I'm having trouble getting nginx to work at all for static files, so I cant test it. Just curious if I am doing it the correct way .
Upvotes: 4
Views: 4663
Reputation: 599450
Your STATIC_ROOT
is ../assets, not ../static, so that is where you should point your nginx location alias for /static.
You have also defined a STATICFILE_ROOT setting to point to ../static, but that is not a Django setting so it does not do anything.
Upvotes: 2