django, static files not found 404 - django

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')

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.

django+gunicorn+nginx gives 404 while serving static files

django+gunicorn+nginx gives 404 while serving static files
I am trying to deploy a Django project using nginx + gunicorn + postgresql. All the configuration is done, my admin panel project static file will serve , but other static files; it returns a 404 error.(iam use run python manage.py collectstatic)
my error.log nginx :: "/blogpy/static/home/test.css" failed (2: No such file or directory)"
Structure:
blogpy
-blogpy
-config
-nginx
-nginx.conf
-docker-compose.yml
-Dockerfile
-home
-static
-home
-test.css(not working)
- requirements
-static
-templates
-.env
-docker-compose.yml
-Dockerfile
setting.py:
DEBUG = False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'
nginx configuration:
---- nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
upstream blogpy{
server blogpy:8000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_pass http://blogpy;
}
location /static/ {
alias /blogpy/static/;
}
}
}
Try
In ---- nginx.conf:
location /static/ {
autoindex off;
alias /home/ubuntu/blogpy/static/; #add full path of static file directry
}
To get /blogpy/home/static/ files been copied into /blogpy/static/ by collectstatic command, you need to specify STATICFILES_DIRS setting
https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATICFILES_DIRS
STATICFILES_DIRS = [
BASE_DIR / 'home' / 'static',
]
It is recommended to serve static files directly from nginx.
Add the following to your nginx site config.
location /static/ {
alias /path/to/static/directory/;
}

Nginx unable to load static files from django

family, Im having a little trouble to make nginx server load static file collected by django. here is my nginx sites-available
server {
listen 80;
server_name <my_ip_address>;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
}
location /asset/ {
autoindex on;
alias /var/www/html/dev2_assets/;
}
}
Down here is my Django STATIC_URL and STATIC_ROOT configurations
STATIC_URL = '/assets/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "assets"),
)
STATIC_ROOT = '/var/www/html/dev2_assets/'
When i run the application with ./manage.py runserver its loads all the static files. Any help. Thanks
Your problem is your Location.
Your not specifying a root for it, also in your settings.py your declaring assets but in your location your declaring asset with a missing s. try changing it to something like this:
location /assets/ {
autoindex on;
root /var/www/html/dev2_assets;
}
Also for debugging purposes try added this above location:
error_log /var/log/nginx/error.log;
Then you will get a specific error message about it not being able to retrive your static files.
Lastly are you sure your utilyzing nginx, django, and gunicorn correct?
Here is a copy of my site file for comparison:
# This redirects all incoming traffic on port 80 to 443
server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443;
ssl on;
ssl_certificate /etc/ssl/domain.com.chained.crt;
ssl_certificate_key /etc/ssl/domain.com.key;
server_name helius.dk;
access_log /var/log/nginx/nginx.vhost.access.log;
error_log /var/log/nginx/nginx.vhost.error.log;
#location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/projectname/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/projectname/gunicorn.sock;
}
}

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.

Can't serve statics with Nginx on DigitalOcean VPS for Django

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