Not Found: /media/ 404 77 Django production - ASGI

2019-08-29 01:18发布

I've tried everything but my media folder profile pic images still aren't appearing. They look like this on the webpage.

broken

I'm in production for my Django 2.1 app, using Digital Ocean running a ASGI server (as I'm using channels).

My media folder is located in my root folder (same level as manage.py).

The error being given is

xx.xxx.xxx.xx:xxxxx - - [23/Feb/2019:17:23:49] "GET /media/profile_pics/avril.jpg" 404 77

But that is the correct path and the image is located there. All of my static files are rendering fine.

my settings.py

ASGI_APPLICATION = 'sobr.routing.application'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

STATIC_URL = '/static/'

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

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'appname/static'),
)

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

MEDIA_URL = '/media/'

CHANNEL_LAYERS = {
    "default": {
        "BACKEND": "channels_redis.core.RedisChannelLayer",
        "CONFIG": {
            "hosts": [os.environ.get('REDIS_URL', 'redis://xx.xxx.xxx.xx:8080')],
        },
    },
}

urls.py

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

nginx upstream

   location /static/ {
        root /home/user/appname/src/appname;
    }

    location /media/  {
        alias /home/user/appname/src/appname/media;
    }

traceback

2019-02-24 18:42:02,932 WARNING  Not Found: /media/profile_pics/girl.jpg
2019-02-24 18:42:02,934 WARNING  Not Found: /media/profile_pics/avril.jpg
2019-02-24 18:42:02,933 WARNING  Not Found: /media/profile_pics/man.jpg
xx.141.198.14:59401 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/brad_2G59aPW.jpg" 404 77
xx.141.198.14:59400 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/girl.jpg" 404 77
xx.141.198.14:59402 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/avril.jpg" 404 77
xx.141.198.14:59399 - - [24/Feb/2019:18:42:03] "GET /media/profile_pics/man.jpg" 404 77
xx.141.198.14:59404 - - [24/Feb/2019:18:42:03] "GET /static/fonts/PermanentMarker-Regular.ttf" 304 -
xx.141.198.14:59405 - - [24/Feb/2019:18:42:03] "GET /static/fonts/SourceSansPro-Light.ttf" 304 -
xx.141.198.14:59401 - - [24/Feb/2019:18:42:03] "GET /static/fonts/Poppins-SemiBold.ttf" 304 -

1条回答
神经病院院长
2楼-- · 2019-08-29 01:54

Have you setup server properly? Please check your nginx upstream for media file as below.

server {
    listen 80;

    ............

    location /static/ {
        alias /home/project/staticfiles/;
    }
    location /media/ {
            alias /home/project/media/;
    }
.........



}
查看更多
登录 后发表回答