Search code examples
djangonginxgunicorn

Django setting nginx


I ran into a problem while publishing the site. When turned on Debug=False the pictures disappear.

Log nginx error:


2023/03/06 11:17:39 [error] 646224#646224: *15 open() "/home/egor/mysite/PersonalPortfolio-project/staticportfolio/image/django_certificate-1.png" failed (2: No such file or directory), client: 109.198.191.208, server: zyoger.ru, request: "GET /media/portfolio/image/django_certificate-1.png HTTP/1.1", host: "www.zyoger.ru", referrer: "http://www.zyoger.ru/about"
2023/03/06 11:17:39 [error] 646224#646224: *14 open() "/home/egor/mysite/PersonalPortfolio-project/staticportfolio/image/python_certificate-1.png" failed (2: No such file or directory), client: 109.198.191.208, server: zyoger.ru, request: "GET /media/portfolio/image/python_certificate-1.png HTTP/1.1", host: "www.zyoger.ru", referrer: "http://www.zyoger.ru/about"

Settings nginx:

server {
    listen 80;
    server_name zyoger.ru www.zyoger.ru zyoger.online www.zyoger.online;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/{
    autoindex on;
    alias /home/egor/mysite/PersonalPortfolio-project/static;
}
    location /media/{
    autoindex on;
    alias /home/egor/mysite/PersonalPortfolio-project/static;
}
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

Settings django:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = 'static/'

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

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

I've read a lot of articles and I can't figure it out. How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04


Solution

  • Not knowing the path to where your django app is hosted, I'll guess that STATIC_ROOT AND MEDIA_ROOT need to be updated in settings.py to:

    /home/egor/mysite/PersonalPortfolio-project/static

    instead of being based on your file's basedir;

    but I don't think you want to put static and media in the same place. I'd recommend creating a

    /home/egor/mysite/PersonalPortfolio-project/media

    and update your nginx conf file to point the media alias there.

    Do you have files in /home/egor/mysite/PersonalPortfolio-project/static? If not you may need to run the admin command to move all the static files to the folder. python manage.py collectstatic

    Documentation on collectstatic: https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#