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.
Related
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/)
I am getting an error with static files in nginx
custom_main.css:1 Failed to load resource: the server responded with a status of 404 (Not Found)
THis is my nginx config:
server {
listen 80;
server_name 94.154.13.214;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /root/django_direct/main_app/;
}
location / {
include proxy_params;
proxy_pass unix: /root/django_direct/django_direct.sock;
}
}
What do I do?
upd: settings.py:
STATIC_URL = '/static/'
# STATICFILES_DIRS = [
# os.path.join(BASE_DIR, 'main_app/static')
# ]
STATIC_ROOT = os.path.join(BASE_DIR, 'main_app/static')
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
(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.
Hi I am trying to serve statics using Django and Nginx on a VPS to get my project live. My settings.py includes following.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')
My project folder includes:
static
----static
----static-only
----media
----templates
In Nginx I got following:
upstream gunicorn_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80;
server_name example.com;
keepalive_timeout 5;
client_max_body_size 4G;
access_log /home/projectuser/logs/nginx-access.log;
error_log /home/projectuser/logs/nginx-error.log;
location /static/ {
alias /venv/project/static/static;
}
location /media/ {
alias /venv/project/static/media;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://gunicorn_server;
break;
}
}
}
I can't serve static no matter what and admin panel css is not showing up.
Please advise asap.
Try changing static and media blocks this way:
location /static/ {
root /venv/project/static;
}
location /media/ {
root /venv/project/static;
}
This way, the http://example.com/static/admin/css/dashboard.css will be searched as /venv/project/static/static/admin/css/dashboard.css on the filesystem.
P.S. Also instead of if (!-f $request_filename) it's better to use try_files: http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files