0

After a user has uploaded an image, she's always returns a 404 error. I think this is a Nginx missconfig but I'm not sure of it and after hours of search and tries I still don't understand the issue.

Here's my settings.py :

DEBUG = False
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR,'static'),
)

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

Next, my urls.py :

urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

And my Nginx config file :

location ^/static/ {
    autoindex on;
    alias /home/user/mywebsite/site/static;
}

location ^/media/ {
    alias /home/user/mywebsite/site/media;
}

Here's a image's generated URL : https://www.mywebsite.com/media/CACHE/images/img/venus/758c9f7d70d0c64b5631cc865986de48.jpg (404)

I use Django Imagekit for handling uploaded images. I always founded these URLs weird...maybe ImageKit need some more extra configuration ?

My static and media directories are in /mainappdjango/static, not on root directory. If I don't do that, my static aren't loaded. The image is well uploaded on the server (I can access it with FTP or SSH).

Do you have any idea of the trouble causing these 404 errors ? :/

Thanks a lot!

Arthur C-G
  • 1,481
  • 4
  • 16
  • 23
  • 1
    What’s the path to a media image when you view it via ssh? You say they are in `/mainappdjango/static/media` but nginx is pointing to `mywebsite/site/media`. And you shouldn’t have any media URL in your urls.py. Your Django app isn’t serving media files. – dirkgroten Sep 20 '18 at 21:08
  • Thanks, I removed the line in my urls.py. Here's my path on my server... ~/user/mywebsite/media/CACHE/images/img/venus_kMHza3Y/a154c07683def505960eb63c63ca541e.jpg – Arthur C-G Sep 21 '18 at 21:07
  • What is ~? And why do you have /site/ in between mywebsite and media in your nginx settings? – dirkgroten Sep 21 '18 at 21:10
  • This is my terminal return, so ~ is my home directory. "site" is where my website is stored, for exemple my root script in supervisor is : /home/user/mywebsite/site – Arthur C-G Sep 21 '18 at 21:11
  • You need the full path and need to use that for nginx. – dirkgroten Sep 21 '18 at 21:12
  • I know ~ is your home directory. I’m asking what’s the path of ~ – dirkgroten Sep 21 '18 at 21:13
  • This is /home/user – Arthur C-G Sep 21 '18 at 21:17
  • I've made a mistake : I forgot the main Django app in the nginx path, cause I moved my static+media directories in my main app directory. – Arthur C-G Sep 21 '18 at 21:17
  • Well then just remove the /site/ in your nginx config and it should match. Also inspect your nginx logs if it still doesn’t work, usually in /var/log/nginx – dirkgroten Sep 21 '18 at 21:19
  • 1
    Putting media inside your app directory is a bad idea anyway because usually your app directory needs to be fetched from a VCS like git. And media is not part of the repository – dirkgroten Sep 21 '18 at 21:22
  • Well sorry, I must be brain dead this evening but my full path of my media directory is : /home/user/mywebsite/site/mywebsite/media/ (I've tried a cd ... on it, I'm sure). – Arthur C-G Sep 21 '18 at 21:22
  • Well OK, what directory structure should I use to have both local and online ressources ? When I don't put it in my app subdirectory, I can't load assets online. – Arthur C-G Sep 21 '18 at 21:24
  • 1
    The command is `pwd` to show the full path. Anyway, that’s the path nginx needs. – dirkgroten Sep 21 '18 at 21:25
  • 1
    Sorry, for local youll need the urlpattern I told you to remove. Best is to wrap it in `if settings.DEBUG` so it’s not used for production. Your MEDIA_ROOT can be outside your project directory, no problem. Also works with local `runserver` – dirkgroten Sep 21 '18 at 21:33
  • I make a cd in a terminal to my media directory and paste exactly what I've got with pwd. For Stack I've changed my user name or so and it's confusing, sorry for that, I need to make the point : My root directory in Nginx is : /home/chuck/domi/site/domi My static directory is : alias /home/chuck/domi/site/domi/static/; My media directory is : alias /home/chuck/domi/site/domi/media; – Arthur C-G Sep 21 '18 at 21:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/180555/discussion-between-zoloom-and-dirkgroten). – Arthur C-G Sep 21 '18 at 21:36
  • 1
    When you ask questions on SO please make sure you copy paste code and configs. Don’t edit them. How do you want us to help fix things if the code you’re showing isn’t the one you’re using? So now you’re saying the alias in nginx is exactly the same as the path to media on your server? Then look at the nginx logs and check for errors there. – dirkgroten Sep 21 '18 at 21:40
  • Really sorry for that...I check the Nginx logs ! – Arthur C-G Sep 21 '18 at 21:42
  • 2018/09/21 23:40:37 [error] 1480#1480: *3 open() "/home/chuck/domi/site/domi/media/CACHE/images/img/venus_kMHza3Y/a154c07683def505960eb63c63ca541e.jpg" failed (2: No such file [...] – Arthur C-G Sep 21 '18 at 21:44
  • So my new config is : location /media/ { autoindex on; alias /home/chuck/domi/site/domi/media/; } – Arthur C-G Sep 21 '18 at 21:45
  • OK, got it. The image disappears in CACHE. I think my GIT config could explains that. So...time to do the things right, I will totally rebuild my project for having something clear. Thanks for your help !! – Arthur C-G Sep 21 '18 at 21:52

1 Answers1

0

you have the wrong nginx config. have to add salsh.

location /static/ {
    alias /home/user/mywebsite/site/static/;
    autoindex on;
}
location /media/{
  alias /home/user/mywebsite/site/media/;
  autoindex on;
}
aman kumar
  • 3,086
  • 1
  • 17
  • 24