Report this

What is the reason for this report?

Django, static folder and files not load and 502 Bad Gateway nginx/1.4.6(Ubuntu).

Posted on March 8, 2015

Hi all,

I am new with digitalocean, I have loaded all my files to the server unfortunately not working like on own pc. The issues I have are do not reload the static files (CSS, media, Js). I have read all comments some how can’t find the error I had only one but not sure about.

Mapping Project:

rootproject
|
├── bichonproject
├── main
│   └── templates
│   └── main
├── static
│   ├── bootstrap
│   │   ├── css
│   │   ├── fonts
│   │   └── js
│   ├── css
│   ├── images
│   ├── js
│   └── media
└── templates

/etc/init/gunicorn.conf

exec gunicorn \
--name=bichonproject \
--pythonpath=rootproject \
--bind=0.0.0.0:9000 \
--config /etc/gunicorn.d/gunicorn.py \
bichon_project.wsgi:application

/etc/nginx/sites-enabled/django

# Your Django project's media files - amend as required
location /media {
alias /home/django/rootproject/bichonproject/static/media;
}

# your Django project's static files - amend as required
location /static {
    alias /home/django/root_project/bichon_project/static; 
}
setting.py

STATICURL = '/static/'
STATICFILESDIRS = (os.path.join(BASEDIR, "static"),)
TEMPLATEDIRS = (os.path.join(BASE_DIR, "templates"),)

CRISPYTEMPLATEPACK = 'bootstrap3'

Another issue it only work when I run:

sudo python manage.py runserver localhost:9000 after killing server display 502 Bad Gateway nginx/1.4.6(Ubuntu).

using Ubuntu Django on 14.04 and the IPAddress 178.62.73.238

Last question do I need to create newvenv files before create app? virtualenv newvenv, if yes do I need to add to my root_project?

Note: setting.py file under bichon_project and main is home app.

Any help very appreciate.

Thanks



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

What i have to do is to move static folder to some of /var/www/static_folder because nginx(or user who is connecting from internet) doesnt have rights to access static files in root.

Nice one

While this question has a valid answer here is a fully explained way to setup static files and prevent such errors

Heya,

To anyone stumbling upon this, here is a full revisited answer.

To configure Nginx to serve static files and media files for a Django web application, you’ll need to make sure your Django project is correctly configured for serving static and media files, and then set up Nginx to handle these files efficiently. Here are the steps to achieve this:

  1. Configure Django for Static and Media Files: In your Django project’s settings (typically found in the settings.py file), make sure you have the following configurations set correctly:
# settings.py

# Define the URL path for static files (CSS, JavaScript, etc.).
STATIC_URL = '/static/'

# Define the absolute filesystem path to the directory that will hold your static files.
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# Define the URL path for media files (user-uploaded files).
MEDIA_URL = '/media/'

# Define the absolute filesystem path to the directory where media files will be uploaded.
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

Ensure that you’ve also added the following line to your project’s urls.py to serve media files during development:

from django.conf import settings
from django.conf.urls.static import static

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
  1. Install Nginx: If you haven’t already, install Nginx on your server:
sudo apt-get update
sudo apt-get install nginx
  1. Create an Nginx Configuration File for Your Django Project: Create an Nginx configuration file for your Django project in the /etc/nginx/sites-available/ directory. Replace your_project_name with a meaningful name for your project:
sudo nano /etc/nginx/sites-available/your_project_name
server {
    listen 80;
    server_name your_domain.com; # Replace with your domain or IP address

    location /static/ {
        alias /path/to/your/static/root; # Replace with the actual path to your STATIC_ROOT
    }

    location /media/ {
        alias /path/to/your/media/root; # Replace with the actual path to your MEDIA_ROOT
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/path/to/your/project.sock; # Replace with your project's Unix socket
    }

    location /favicon.ico {
        alias /path/to/your/static/root/favicon.ico; # Replace with the path to your favicon
    }

    location /robots.txt {
        alias /path/to/your/static/root/robots.txt; # Replace with the path to your robots.txt file
    }

    error_page 500 502 503 504 /static/500.html;
}

Save the file and exit the text editor.

  1. Enable the Nginx Configuration and Test: Create a symbolic link to enable the site configuration and then test the Nginx configuration:
sudo ln -s /etc/nginx/sites-available/your_project_name /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
  1. Adjust Permissions: Make sure that the Nginx user (www-data on Ubuntu) has read access to your static and media directories. You can adjust permissions with the following commands:
sudo chown -R www-data:www-data /path/to/your/static/root 
sudo chown -R www-data:www-data /path/to/your/media/root

That’s it! Nginx should now be correctly configured to serve your Django project’s static and media files. Make sure to replace placeholders like your_domain.com, /path/to/your/static/root, /path/to/your/media/root, and /path/to/your/project.sock with your actual values.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.