0

I followed this tutorial to deploy my Django project on DigitalOcean. I've installed Nginx and Supervisor and all worked until I set DEBUG option in the settings.py to False

I tried to configure nginx.conf and settings.py million times. Changing root to alias wouldn't help.

Nginx configuration file:

upstream app_server {
  server unix:/home/db1/run/gunicorn.sock fail_timeout=0;
}

server {
  listen 80;

  # add here the ip address of your server
  # or a domain pointing to that ip (like example.com or www.example.com)
  server_name ...;

  keepalive_timeout 5;
  client_max_body_size 4G;

  access_log /home/db1/logs/nginx-access.log;
  error_log /home/db1/logs/nginx-error.log;
  location /static/ {
    root /home/db1/site1/static;
  }
  # checks for static file, if not found proxy to app
  location / {
    try_files $uri @proxy_to_app;
  }

  location @proxy_to_app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }
}

Settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Luna
  • 193
  • 1
  • 2
  • 10
Blogger 2015
  • 109
  • 1
  • 2
  • 11
  • 1
    Did you run collectstatic? try removing the trailing slash `STATIC_ROOT = os.path.join(BASE_DIR, 'static')`. If your `STATIC_DIR` is `static`, you should change the name to something different than `static` to keep STATIC_ROOT and STATIC_DIR different – Sergio Feb 03 '19 at 10:40
  • Thanks. I removed slash and it didn't do magic. And I've run collectstatic like hundred times with different paths. Also I have copy of my static folder in three different places inside my project but that doesn't help. I don't have STATIC_DIR parameter in my settings and the tutorial's also lacking in it. There is only STATIC_URL – Blogger 2015 Feb 03 '19 at 11:20
  • It worked when debug was set to true? – Thomas Myers Feb 03 '19 at 13:30
  • Yes, it did work. Anyway, I found a solution. I think my mistake was that I was running with the development port 8000 attached to the address, so to be able to load static files I needed to remove port. Still, don't know exactly if it was the main issue as I made several adjustments following tutorial from DigitalOcean itself https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04 – Blogger 2015 Feb 03 '19 at 14:05
  • I should mention that there is a mistake in nginx.conf. When you use 'root' instead of 'alias' you should exclude the ending folder and it must be ` root /home/db1/site1/`. It's explained pretty well [here](https://stackoverflow.com/questions/10631933/nginx-static-file-serving-confusion-with-root-alias) – Blogger 2015 Feb 03 '19 at 14:10

2 Answers2

0

It's quite tricky, just use one of those two lines according to status of DEBUG

STATIC_ROOT = os.path.join(BASE_DIR, 'static/') #Production


STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static/'), ) #Development
Ramy M. Mousa
  • 5,727
  • 3
  • 34
  • 45
0

Here is a few steps that helped me in solving this problem. Don't know exactly which one is an actual solution)

  • Changing the owner of static folders to a recently created user (a newly created user for new project like in tutorial)
  • Connection to the server via IP address only (without PORT)

Still, I don't get it why adding a PORT will cause static files to be held at server

Blogger 2015
  • 109
  • 1
  • 2
  • 11