0

Edit: I've made some progress trouble shooting this issue. For an update on the situation, read the comments and view this post on ServerFault: https://serverfault.com/questions/690836/django-uwsgi-nginx-not-serving-media-files-django-returns-404-status-code?noredirect=1#comment851441_690836

I'm following this tutorial: http://uwsgi-docs.readthedocs.org/en/latest/tutorials/Django_and_nginx.html

Everything was going well until I reached the "Basic nginx test" section. When I stopped and started nginx and then added "media.png" to my media folder and went to

192.168.***.***:8000/media/media.png 

it gave me a 404 status code. Note that this status code was given by Django (DEBUG=True), so it says:

Page not found (404)
Request Method: GET
Request URL:    http://192.168.***.***:8000/media/media.png
Using the URLconf defined in CMS.urls, Django tried these URL patterns, in this order:

This is mysite_nginx.conf:

# mysite_nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name 192.168.***.***; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /home/a/Documents/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/a/Documents/CMS/CMSApp/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/a/Documents/CMS/CMS/uwsgi_params; # the uwsgi_params file you installed
    }
}

Any idea why I'm getting a 404 error when trying to view the media file?

Additional information: When I go to

http://192.168.***.***:8001/media/media.png

or

127.0.0.1:8001

it says "This web page is not available".

Community
  • 1
  • 1
SilentDev
  • 20,997
  • 28
  • 111
  • 214
  • Have you defined `MEDIA_URL` and `MEDIA_ROOT` in your [settings.py](https://docs.djangoproject.com/en/1.8/ref/settings/#media-url)? – akarilimano May 09 '15 at 05:22
  • 1
    @akarilimano I haven't, I don't think I came across that step in the tutorial yet. The tutorial mentioned for me to add "STATIC_ROOT = os.path.join(BASE_DIR, "static/")" and then do "python manage.py collectstatic" but didn't mention defining MEDIA_URL and MEDIA_ROOT yet. Should I define it? If yes, I'm assuming I make MEDIA_URL = /media/ and MEDIA_ROOT = os.path.join(BASE_DIR, "../media/") (that's where my media folder is)? – SilentDev May 09 '15 at 05:43
  • I suppose so. Django should know where your media files are. – akarilimano May 09 '15 at 05:49
  • @akarilimano I'm going to wait and see if anyone else has another answer before trying to define media_root and media_url in settings.py (since the tutorial doesn't mention it). – SilentDev May 09 '15 at 13:54
  • @akarilimano I added MEDIA_ROOT and MEDIA_URL in my settings.py file but Django still gives me the 404 error. – SilentDev May 09 '15 at 17:21
  • What are UNIX permissions on your media directory? Does nginx has rights to read from it? As for MEDIA_ROOT/MEDIA_URL I misunderstood your question, now I think you don't really need them. – akarilimano May 10 '15 at 14:56
  • @akarilimano okay so the /media/ folder and the "media.png" file both have read access by anyone. When I change "listen 8000;" to "listen 8001;" and go to "127.0.0.1:8001/media/media.png" then the image shows up (this is when Django runs on :8000). But according to the tuotirlal, if I go to 127.0.0.1:8000 then Django should return the page and if I go to 127.0.0.1:8000/media/media.png then Nginx should serve the file. Any idea on how I can accomplish that? – SilentDev May 10 '15 at 17:07
  • @akarilimano I've made some progress trouble shooting this issue. For an update on the situation, read the comments and view this post on ServerFault: http://serverfault.com/questions/690836/django-uwsgi-nginx-not-serving-media-files-django-returns-404-status-code?noredirect=1#comment851441_690836 – SilentDev May 10 '15 at 17:49

0 Answers0