Nginx not serve media files - django

I develop an application with Django and Angular 4, before I can see media files without problems in my localhost, but in my LightSailServer I delete my sock file created with gunicorn and deploy my app again, but I don't understand why I don't see my media files in this moment, this is my nginx file, but I don't touch this in all moment, I think my problem is in other way, any ideas?
server {
listen 80;
server_name 54.12.12.142;
location = /favicon.ico {
alias /home/ubuntu/tracker-web/trackerServer/favicon.ico;
}
include /etc/nginx/mime.types;
location /static/ {
root /home/ubuntu/tracker-web/trackerServer;
}
location /media/ {
autoindex on;
alias /home/ubuntu/tracker-web/trackerServer/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/tracker-web/trackerServer/trackerServer.sock;
}
}
I don't see problems in this file, but I can't see my media files and these exists!
PD: I obtain correct path to media files from my web server, only I can't see these.
This I have in my settings.py
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static-root')
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Thanks
SOLVED
I had deleted the image folder from my server

Related

django nginx not serving static files

I am depploying django using nginx and gunicorn. When I access the website I get the following errors.
open() "/home/x/aroundu/core/static/static/rest_framework/js/default.js" failed
it's accessing static files wrong, because the path should be like this
open() "/home/x/aroundu/core/core/static/rest_framework/js/default.js" failed
server {
listen 80;
location /static/ {
alias /home/x/aroundu/core/core/static/;
}
location /media/ {
alias /home/x/aroundu/core/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = "/media/"
This should be:
location /static/ {
root /home/x/aroundu/core/core/;
}
location /media/ {
root /home/x/aroundu/core/;
}
Also for static you have /core/core/ and for media you have only /core/. I don't know which is the right one, but given your settings they should be the same.

Statics problem with django docker nginx gunicorn

I have a small problem with access to my django statics through nginx.
I serve the django server running in a docker, on a url with a location in nginx:
location /external/ {
proxy_pass http://external;
}
For static configuration in nginx i add:
location /external/static/ {
autoindex on;
alias /external/static/;
}
For the configuration of my statics on Django I set this code in the settings:
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/' + PREFIX + 'static/' ## -> with my config = /external/static/
In my Dockerfile I run my django server with gunicorn and I expose it through port 8000. When I access directly to the server with port 8000 (http://serveraddr.com:8000) I don't have statics problems but when I access it with nginx (https://serveraddr.com/external) I get a not found on my statics.
Help me please
Assuming your static files are in static directory
location /external/static/ {
autoindex on;
alias /full/path/to/folder/static;
}

Django on nginx ubuntu server static files in admin not found

I have setup my django project on a ubuntu server with nginx webserver.
In my django settings.py file i set:
STATIC_URL = '/static/'
STATIC_ROOT = '/static/images/'
STATICFILES_DIRS = [
'/var/www/core/frontend/static',
'/.venv/lib/python3.6/site-packages/django/contrib/admin/static',
]
whell, in my nginx.conf file i write:
server {
listen 80 default_server;
listen [::]:80 default_server;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static/ {
alias /var/www/core/frontend/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
}
but when i open the admin part of my project i get a lot of 404 error about css and js not found.
Where i have to implement my code for correctly manage static admin file?
So many thanks in advance

Unable to load staticfiles after deploying django app on aws-ec2

I am a beginner in aws and I am trying to deploy a django app on aws-ec2. I have set-up gunicorn application server and nginx web server and app loads but without static files. I have followed many answers on stackoverflow but I am unable to fix my problem.
I tried with both root and alias but they didn't work. The project structure is as follows:-
/home/ubuntu/myskillhut/
django.conf(nginx configuration file)
server {
...
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/myskillhut/app.sock;
}
location /static/ {
autoindex on;
alias /home/ubuntu/myskillhut/static/;
}
}
settings.py
...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static/"), )
...
Configuration below works for my project
location ~* \.(jpe?g|gif|png)$ {
root /home/ubuntu/myskillhut/static/;
expires 1M;
}
location ~* \.(css|js)$ {
root /home/ubuntu/myskillhut/static/;
expires 1d;
}
Also, make sure nginx can access those files in that dir (../static/)

Nginx - Django i serve perfect static files but not media files

(Sorry for my bad english)
I'm having a peculiar problem... I configure Django and Nginx all perfect, my site shows perfect all static files are loaded perfect to. But..... Nginx is not serving the media files, i check my configurations and all seems to be ok. But i always get the 404 error and the route is the same that static files but with the media word
this is a static file route
http://project.com/static/css/custom.css
And this a media file
http://project.com/media/stores/logos/solutions_logo_rdWRcqQ.jpg
This is the nginx config
server {
listen 80;
server_name project.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/username;
}
location /media/ {
root /home/username;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/username/project.sock;
}
}
And this are the django settings
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static/'),
)
STATIC_ROOT=os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'static/')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(os.path.dirname(BASE_DIR)), 'media/')
Any idea???
A guess into the wild. Your static files are served directly by django and not by your nginx. (You can test that by removing the location /static { ... } section and restart your nginx.)
Then to fix your problem please try this:
server {
listen 80;
server_name project.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/username;
}
location /media/ {
alias /home/username;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/username/project.sock;
}
}
Also, dont forget to restart your nginx afterwards.