5

I am testing my django app in production mode (debug=false) using nginx, gunicorn, postgresql.

Though I am able to render static files, I am unable to access files stored in 'media' folder.

In my settings.py following are the variables set:

 MEDIA_URL = '/media/'
 MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
 # also tried another combination: MEDIA_ROOT = 'media'

Also in urls.py the MEDIA_ROOT settings are as follows:

 urlpatterns = [
   path('admin/', admin.site.urls),
   path('venter/', include('appname.urls')),
 ]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And in my /etc/nginx/sites-available/ file I have the following settings:

server {
listen 80;
server_name website.com www.website.com ;

location = /favicon.ico { access_log off; log_not_found off; }
location /static {
    root /home/a/btawebsite;
}

location /media/ {
    root /home/a/btawebsite;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/home/a/myproject.sock;
}
}

However while rendering the xlsx file stored in my django database as follows it throws me NOT found error.

{{file.output_file.url}}

I have tried every combination of configurations required for rendering MEDIA files but unable to achieve the outcome. Thanks.

UPDATE: following changes to be made in settings.py

 MEDIA_URL = '/'
 MEDIA_ROOT = 'media'
Simran
  • 593
  • 1
  • 14
  • 37
  • You should NOT have `+ static(...)` in your urls.py for production (only for your development environment). Now to debug check this: what is the URL shown in your browser? Then using ssh, cd into the /home/a/btawebsite/media directory and check that's where your uploaded files are located. – dirkgroten Jul 23 '19 at 10:57
  • Please read this: https://docs.djangoproject.com/en/2.2/howto/static-files/deployment/ – bruno desthuilliers Jul 23 '19 at 10:59
  • 1
    @dirkgroten, the media files point to the same directory in both development and production mode; yet it is not getting served – Simran Jul 23 '19 at 10:59
  • @Simran THIS IS ALL DOCUMENTED (cf my comment above) – bruno desthuilliers Jul 23 '19 at 11:00
  • @brunodesthuilliers the django documentation link you provided only provides configurations for serving static files in production mode, not media files – Simran Jul 23 '19 at 11:02
  • @simran I don't have enough information to help, you need to look inside your browser at the src url, you need to confirm that if you ssh and cd into the directory, the file location is correct, you need to confirm that you looked at the nginx logs and checked what url caused the 404 not found. – dirkgroten Jul 23 '19 at 11:04
  • Also why do have `=` between `location` and `/media/`? – dirkgroten Jul 23 '19 at 11:05
  • @dirkgroten correction made. Error while pasting code – Simran Jul 23 '19 at 11:07
  • @Simran mmm yes indeed, I misread your question, sorry - but the principle is the same (`collectstatic` stuff set aside) – bruno desthuilliers Jul 23 '19 at 11:09
  • @Simran then please follow the debug steps I mentioned and report back for each of them. Can't help without knowing these. And did you remove `+ static` from your urls.py? – dirkgroten Jul 23 '19 at 11:14

3 Answers3

5

If everything in django settings is properly configured you just need to add the following in the nginx conf:

location /media  {
   alias /home/user/django_app/media; #(locaion of your media folder)                                                                                       
}
Nijo
  • 772
  • 1
  • 8
  • 27
2

In your settings.py write like this

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')  # root for media files

MEDIA_URL = "/media/"

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

FORCE_SERVE_STATIC = True
DEBUG=False

In your urls.py change like this

if settings.DEBUG:
    urlpatterns += static(
        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
elif getattr(settings, 'FORCE_SERVE_STATIC', False):
    settings.DEBUG = True
    urlpatterns += static(
        settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(
        settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    settings.DEBUG = False

In your nginx.conf file change the root to your media folder

location /media/  {
            root /home/nazmi/workspace/portal/media/ (url for your media folder);                                                                                       
                }
Thusitha Deepal
  • 1,392
  • 12
  • 19
0

First of all, remove the +static() from your urls.py. That's not correct for production, only for development.

In your nginx configuration, location = /media/ only applies for exact matches, not locations starting with /media/. Remove the =.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42