0

Django media file serving in development but not on production. whatever image i am uploading through Django admin it serving on website on local host but when i live my site on digital ocean its no displaying. how to solve this issue can any one tell ? my website url-http://139.59.56.161 click on book test menu

wonea
  • 4,783
  • 17
  • 86
  • 139
Aayush
  • 1
  • 2
    Please elaborate what you already have tried. Do you get any error messages? Please update your post accordingly. – Fjarlaegur Nov 20 '17 at 10:45

1 Answers1

0

Resurrecting a long-dead question which was all I could find to help me out here. Recording my answer for posterity. My "production" environment uses nginx as a reverse proxy in front of uwsgi hosting my django application. The solution is that Django just does not serve files in Production; instead you should configure your web-server to do that.

Django is slightly unhelpful in talking about static files and then saying 'media files: same.'

So, I believe its best to catch file requests up front, in my case in the nginx server, to reduce double-handling and also your front-end web-server is the most optimised for the job.

To do this: within a server definition block in your /etc/nginx/sites-available/[site.conf], define the webroot, the directory on your server's file system that covers everything with the declaration 'root [dir]'.

server {
   listen 80;
   server_name example.com www.example.com;
   root /srv/;

This next block tells nginx to send all the traffic to the uwsgi service running django - I lifted it holus bolus from an example, probably on digitalocean.com.

     location / {
      proxy_pass   http://localhost:8000;
      proxy_set_header   Host   $host;
      proxy_set_header   X-Real-IP  $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_set_header   X-Client-Verify  SUCCESS;
      proxy_set_header   X-Client-DN   $ssl_client_s_dn;
      proxy_set_header   X-SSL-Subject  $ssl_client_s_dn;
      proxy_set_header   X-SSL-Issuer   $ssl_client_i_dn;
      proxy_read_timeout  1800;
      proxy_connect_timeout  1800;
      include uwsgi_params;
      uwsgi_pass   unix:/run/uwsgi/mysite6.sock;
      }

Now, here are the bits we need to serve files when they are requested. try_files attempts to serve $uri and then $uri/, and it would be a good idea to put a file like 'resource_not_found.html' in /srv and set it as the last fallback for try_files, so the user knows that this part has been unintentionally left blank.

location /static/ {
    try_files $uri $uri/ ;
 }
location /media/ {
 try_files $uri $uri/ ;
}
}

That concludes our server block for http, hence the extra close "}".

Alternatively, you can get uwsgi doing it by setting 'static-map' or 'static-map2'. 'static-map' "eats" the mapped url part, whereas static-map2 adds it.

 static-map /files=/srv/files

means a request for /files/funny.gif will serve /srv/files/files.gif.

 static-map2 /files=/srv 

will do the same thing, because it will take a request for /files/funny.gif and look for /srv/files/funny.gif. As per the uwsgi docs, you can create as many of these mappings as you want, even to the same uri, and they will be checked in order of appearance. Damnit, I've just now finally found the docs for nginx open source.

uwsgi docs

Atcrank
  • 439
  • 3
  • 11