Search code examples
djangonginxdjango-templatesnginx-configdjango-staticfiles

Cannot Configure (Error While) Django setting in nginx


My server cannot find the files, it says no files found . I have following configuration

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
       
}

http {

       

        sendfile on;
        tcp_nopush off;
        tcp_nodelay off;
        keepalive_timeout 265;
        types_hash_max_size 2000;

and Server is

server {
    listen 8000;
    server_name joshna.co www.joshna.co joshna.net www.joshna.net;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/{
    autoindex on;
    alias /home/bsyal/mysite/mypro/static;
}
    location /media/{
    autoindex on;
    alias /home/bsyal/mysite/mypro-project/static;
}
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Server cannot find file, I have gone thhrough others solution but could not figured out


Solution

  • Instead of Nginx, it is because of your Django static configuration. You need , static and media files to be served from different directories.

    Create two directories (the directories "/home/egor/mysite/mypro-project/static" and "/home/egor/mysite/mypro-project/media" exist ) if not there already and update your Nginx configuration as follows:

    server {
    listen 80;
    server_name joshna.co www.joshna.co joshna.net www.joshna.net;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias /home/bsyal/mysite/mypro-project/static/;
    }
    location /media/ {
        alias /home/bsyal/mysite/mypro-project/media/;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
    }