marcosgue
marcosgue

Reputation: 697

Nginx - Django i serve perfect static files but not media files

(Sorry for my bad english)

I'm having a peculiar problem... I configure Django and Nginx all perfect, my site shows perfect all static files are loaded perfect to. But..... Nginx is not serving the media files, i check my configurations and all seems to be ok. But i always get the 404 error and the route is the same that static files but with the media word

this is a static file route http://project.com/static/css/custom.css

And this a media file http://project.com/media/stores/logos/solutions_logo_rdWRcqQ.jpg

This is the nginx config

server {
    listen 80;
    server_name project.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            root /home/username;
    }

    location /media/ {
            root /home/username;
    }

    location / {
            include proxy_params;
            proxy_pass http://unix:/home/username/project.sock;
    }
}

And this are the django settings

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT=os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'static/')

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

Any idea???

Upvotes: 0

Views: 987

Answers (1)

chickahoona
chickahoona

Reputation: 2034

A guess into the wild. Your static files are served directly by django and not by your nginx. (You can test that by removing the location /static { ... } section and restart your nginx.)

Then to fix your problem please try this:

server {
    listen 80;
    server_name project.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
            alias /home/username;
    }

    location /media/ {
            alias /home/username;
    }

    location / {
            include proxy_params;
            proxy_pass http://unix:/home/username/project.sock;
    }
}

Also, dont forget to restart your nginx afterwards.

Upvotes: 1

Related Questions