Home:ALL Converter>nginx how to serve pictures or media files?

nginx how to serve pictures or media files?

Ask Time:2018-01-23T09:56:24         Author:Kyle

Json Formatter

I'm having issues serving pictures with nginx. I originally started with a Django project and I wanted to serve the user uploaded media files via nginx but I wasn't able to get it working no matter what I tried.

So, I've make a second temporary droplet/server and am trying a bare bones setup with no Django project, just Nginx, to see if I can get it to simply serve an index and a couple pictures in a folder called 'media'. Here is the server block:

    server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html;

        server_name 159.89.141.121;

        location / {
                try_files $uri $uri/ =404;
        }

        location /media/ {
                root /var/www/example.com/media;
        }

}

Now, the index.html is served fine but if I try to go to 159.89.141.121/media/testpic.jpg it still doesn't work and returns a 404 error. I'm at a complete loss here, I've tried using alias instead of root, I've tried changing the folder structure and setting all permissions to 777 and changing folder and file ownership, permissions shouldn't be a problem because the index.html works fine with the same permissions; I just cant seem to figure out what I'm doing wrong. The picture is in the folder but nothing I try allows me to access it via the uri. Are there any obvious problems with my server block?

Author:Kyle,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/48393037/nginx-how-to-serve-pictures-or-media-files
yy