Static files not updating on prod - django

I am new to production stuff. I deployed my first Django app on digital ocean . It's working.
The code is updating fine but the problem is with the static files which are serving through nginx.
I updated the static files and restart nginx and gunicorn but they are not present in my prod website.
I checked with chrome dev tools and it's serving old static files.
I checked these changes are present in the prod repo but somehow nginx don't seem to be using the latest static files . The steps I took
Run collectstatic command
Restart the nginx
Restart the gunicorn
The nginx config for static files
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/djangoadmin/pyapps/hkc_project;
}
This is static settings:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'hkc/static')
]
Is there anything else I have to do for this to work?
Thanks in advance

As far as I know nginx, the best way to serve the static files with it is:
location /static {
autoindex on;
alias /home/djangoadmin/pyapps/hkc_project;
}
alias is the proper method to serve entire directories.
autoindex processes requests ending with the slash character (‘/’) and produces a directory listing.

Related

Gunicorn does not show Django static files

I've seen this question asked before but none of the responses have helped.
I'm trying to deploy my Python3/Django project to AWS with gunicorn + nginx. The project name is vicver, and when I try to run
$ gunicorn --bind 0.0.0.0:8000 vicver.wsgi
I can see my site on the AWS public IP port 8000 but without any js/css or images. When I do the same with the runserver command it works fine.
My settings.py contains the following paths:
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [ os.path.join(BASE_DIR,'vicver/static') ]
And here's a screenshot of the terminal
https://imgur.com/a/KhgcXeT
It works when you run runserver command because you're in development mode. But when you want to deploy your application to production env, you should use some of these options to serve static files Deploying static files
Normally I use Nginx to serve static files, and create a reverse proxy to proxy other requests into the app which served by Gunicorn. You can take a look on this Deploying django application to serve your app by using Gunicorn too.
You can not serve your static and media file in the production mode(Debug=False). The best way to serve statics and media with this structure is by using Nginx.
First, you need to collect your statics using the following command:
python manage.py collectstatic
Then you need to edit your "nginx.conf" file using the following codes. Be careful to replace "/usr/src/app/" with your project path.
server {
listen 80 backlog=2048;
location = /favicon.ico { access_log off; log_not_found off;}
location /static/ {
alias /usr/src/app/vicver/static/;
}
location /media/ {
alias /usr/src/app/vicver/media/;
}
.
.
.
}

Serving static and media files on a distant server with Django and Nginx

I am configuring a server with Nginx that redirect subdomains to websites (made with Django) on distant servers (on the same local network). It is working fine to serve the content of each site, but I have trouble to serve static and media files (for instance css). Here is the content of the configuration file :
server {
listen 80;
server_name myaddress.fr
location / {
proxy_pass http://192.168.0.85:8000;
}
}
And here is the end of settings.py in the Django website (which listen at 192.168.0.85:8000) :
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'
Usually, when my websites are on the same server than nginx, I just have to add these lines to the nginx conf file:
location /media/ {
alias /local/path/to/media/;
}
location /static/ {
alias /local/path/to/static/;
}
How can I generalize this config to serve media and static files on a distant server (here at 192.168.0.85)? Should I install another webserver on the distant machine?
Thanks in advance for your help!
You need some way of providing a route to show those files.
One way of doing so would be to install nginx on the remote servers and proxy to it just as you do with the app itself.
An alternative, since you say this is all within the same local network. would be to use something like NFS to mount the static directories onto the proxy server, so that the existing nginx could serve them directly.
A similar option would be to configure the staticfiles app, as well as the storage of user-uploaded files, to save their files directly on the proxy.

Why is static content request going to uwsgi?

I'm setting up my Django project to work with uwsgi and nginx. For static content, I have the following in my nginx.conf:
location /static {
alias /Users/me/mystatic; # your Django project's static files - amend as required
}
I have set STATIC_ROOT to /Users/me/mystatic and called collectstatic to copy all the static files into that directory. In my uwsgi log, I see GET requests for the static content. Since nginx is supposed to serve the static content, why is the GET request sent to uwsgi?
Make sure that STATIC_URL is set as "/static/".
The nginx conf should look something like this:
server {
listen 80 ;
server_name XXXX;
client_max_body_size 4G;
location /static/ {
alias <path-to-collectstatic>
}
location / {
include uwsgi_params;
uwsgi_pass unix:/tmp/uwsgi_web.sock;
}
}
Notice the appending / in location block for static
Last thing, make sure that application server is running with DEBUG as False
The issue was that I had placed my own nginx.conf in /usr/local/etc/nginx/sites-enabled, not /usr/local/etc/nginx/servers. In /usr/local/etc/nginx/nginx.conf, include servers/*; is at the end. Therefore, UWSGI was still serving the static files. I followed the django-nginx-uwsgi tutorial, and I assumed I had to create a new sites-enabled directory. I am using OS X.
I see on Linux, though, /etc/nginx/nginx.conf has include include /etc/nginx/sites-enabled/*; at the end, so the steps in the tutorial are applicable on Linux.
The extra / at the end of /static doesn't make a difference, but it doesn't hurt to have it at the end of all paths.

Nginx is not serving Django static files

I just configured my first django server for a very basic django-website. I'm using Django 1.4 and Nginx 1.0.14 with supervisor and gunicorn.
The problem is that Nginx is not serving the static files as suposed, but I don't know the why because is my first time using it.
This is the path where really lives my static files:
/home/cristian/envs/santalupe.com/santalupe/santalupe/static
And this is the setting I've in my nginx config file:
# Django admin media.
location /media/ {
autoindex on;
alias /home/cristian/envs/santalupe.com/lib/python2.7/site-packages/django/contrib/admin/static/;
}
# Site media
location /static/ {
autoindex on;
alias /home/cristian/envs/santalupe.com/santalupe/santalupe/static/;
}
Please let me know what I need to do in this case because I have not idea about the real problem.
You aren't serving the admin media from Nginx, just normal media. Try something like:
location /admin/media/ {
# this changes depending on your python version
root /home/cristian/envs/santalupe.com/lib/python2.7/site-packages/django/contrib;
}
Note how there's no trailing slash and the path ends at contrib. I'm using almost exactly this code successfully in production.
Maybe manage.py collectstatic on server help you?
Here is good description on right solution https://docs.djangoproject.com/en/dev/howto/static-files/#serving-static-files-in-production

Add expires header to django with nginx

I use django_compress to compress my static files. All static files are collected under /static/ url using collectstatic command. Now I want to add expires headers to this files. There is no problem to do this with apache, but when I add to my nginx.conf:
location /static/ {
root /home/user/proj/static;
expires 7d;
}
and restart the server then static files are not served. What should I change?
The location is under server context.
Refs http://wiki.nginx.org/HttpCoreModule#root, a request such as http://yourserver/static/foo will be directed to /home/user/proj/static/static/foo . Is the static/static right structure on you server?
Also run nginx -t to ensure there is no error in configuration, before reload nginx.