How to fix this path issue?

I have a django app running with nginx and gunicor.
The images, CSS and JS files are not loading! When I was checking the error log file (/var/log/nginx/error.log) I could see the path the machine is considering is different than the one I’ve typed!!! One of the errors, but they are all the same:

[error] 25420#25420: *3487 open() "/home/ubuntu/petangel/staticfilesgeneral_template.css" failed (2: No such file or directory), 

but the path is: /home/ubuntu/petangel/staticfiles/general_template.css

on the other hand in the /etc/nginx/sites-enabled/default:

location /static/ {
        alias /home/ubuntu/petangel/staticfiles;
        }

        location /images/ {
        alias /home/ubuntu/petangel/crud/media/media;
        }

        location = /favicon.ico {
        alias /home/ubuntu/petangel/crud/static/iconscat.png;
        }

in settings.py:

STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, '/home/ubuntu/petangel/staticfiles')
STATICFILES_DIRS = [    
    os.path.join(BASE_DIR, 'users/static/'),
    os.path.join(BASE_DIR, 'crud/static/'),
]

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

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

how would I fix this “miss type”?
thank you in advance

Side note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then the code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post for this.)

Unless I’m missing something here, you should be able to change this:

To this:

        alias /home/ubuntu/petangel/staticfiles/;

(This should also be done for your /images/ alias.)

See the docs and examples at Module ngx_http_core_module

Side note 2: I never recommend granting permissions into a home directory to nginx. I always recommend that in a deployment environment that both static and media directories reside completely outside the directories in which the code itself is stored.
See my comment at Django and Nginx permission issue on Ubuntu - #2 by KenWhitesell for more information about this.

Hello Ken
Thank you so much for your help, the static files are working already! :smiley:
But for images, in my project calls media, is there any additional info should I provide beyond these ones:

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

and in nginx config:

        location /static/ {
        alias /home/ubuntu/petangel/staticfiles/;
        }

        location /images/ {
        alias /home/ubuntu/petangel/crud/media/media/;
        }

        location = /favicon.ico {
        alias /home/ubuntu/petangel/crud/static/iconscat.png;
        }

I am asking you that, because it is not working still the issue is the same, but I did the same thing but nothing… :confused:
really thank you for your help… :slight_smile:

Your alias has an extra “media/” in it. Also, you’ve defined the MEDIA_URL = 'media/', but your location directive is for /images/.

Hi Ken
Actually the directory structure is like that, it has media/media. But the question here is: Can’t I use the directive /media/ for image files?

thank you once again! :slight_smile:

I don’t understand what you mean here.

Your MEDIA_URL doesn’t match your location directive. Your media references are going to generate a url that you don’t have a location directive for.

I meant: my images files are located in:
/home/ubuntu/petangel/crud/media/media

also I do have these configs:

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

and

```location /static/ {
        alias /home/ubuntu/petangel/staticfiles/;
        }

        location /images/ {
        alias /home/ubuntu/petangel/crud/media/media/;
        }

        location = /favicon.ico {
        alias /home/ubuntu/petangel/crud/static/iconscat.png;
        }```


but the images are not loading...

Yes, because this:

does not match (is not the same as) this:

Is one simple solution just change:
location /images/
to
location /media/

Because I did and still… :confused:

Then you need to show the details of the results. Show the log file from the server and the console log in the browser where it’s trying to retrieve those files, along with the change that you made to the nginx configuration file.

Ken…
now I got the problem and I could solve! Everything is 100% :slight_smile:

thank you very much for your help
have a nice day