nginx doesn't acess folders inside static folder (django app) - django

I searched and didn't find this exact problem.
I deployed an application in django on GCP, using gunicorn + nginx
It's working normally, except for the static files
I ran django's collectstatic and got the files folder and statics like this:
lnmat#sistema-ag:~/app_repo$ ls
apps gunicorn-error-log requirements.txt static venv
base_static manage.py sistema_vendas_ag templates
lnmat#sistema-ag:~/app_repo$ cd static
lnmat#sistema-ag:~/app_repo/static$ ls
admin global
lnmat#sistema-ag:~/app_repo/static$ ls admin
css fonts img js
lnmat#sistema-ag:~/app_repo/static$ ls global
css js vendor
In the nginx error log:
2022/12/13 20:36:00 [error] 8136#8136: *1 open() "/home/lnmat/app_repo/static/js/sb-admin-2.min.js" failed (2: No such file or directory), client: x.x.x.x, server: -, request: "GET /static/js/sb-admin-2.min.js HTTP/1.1", host: "x.x.x.x", referrer: "http://x.x.x.x/login/?next=/"
2022/12/13 20:36:09 [error] 8136#8136: *7 open() "/home/lnmat/app_repo/static/css/sb-admin-2.min.css" failed (2: No such file or directory), client: x.x.x.x, server: -, request: "GET /static/css/sb-admin-2.min.css HTTP/1.1", host: "x.x.x.x", referrer: "http://x.x.x.x/"
2022/12/13 20:36:09 [error] 8136#8136: *12 open() "/home/lnmat/app_repo/static/vendor/jquery/jquery.min.js" failed (2: No such file or directory), client: x.x.x.x, server: -, request: "GET /static/vendor/jquery/jquery.min.js HTTP/1.1", host: "x.x.x.x", referrer: "http://x.x.x.x/"
Apparently nginx is trying to access the static files directly in the main folder without accessing the subfolders global/ and admin/ which is where the files are.
My nginx server block:
server {
listen 80;
server_name x.x.x.x;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /static/ {
autoindex on;
alias /home/lnmat/app_repo/static/;
}
}
My settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'base_static',
]
STATIC_ROOT = BASE_DIR / 'static/'
I don't know what it could be, any suggestions?
[EDIT]
My static tags in template:
{% load static %}
...
<link href="{% static 'css/sb-admin-2.min.css' %}" rel="stylesheet">
<link href="{% static 'vendor/fontawesome-free/css/all.min.css' %}" rel="stylesheet" type="text/css">
...
My base_static folder:
image

Related

How to properly configure static folder for django's admin files

I have deployed project on production (django+gunicorn+nginx)
My project structure is as follows
forecast # this dir of os.path.join(Base_Dir)
manage.py
forecast
settings.py
urls.py
static
admin
css
fonts
img
css
js
In settings.py static folder configured as follows
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = [os.path.join(BASE_DIR,'static')]
While i am trying to login to admin page it's return me 500 server error. Than i tried to see what in my nginx logs /var/log/nginx/error.log.1
There are following
2020/02/20 13:01:53 [error]
11703#11703: *5 open() "/usr/share/nginx/home/isli/projects/forecast/static/static/admin/css/base.css"
failed (2: No such file or directory),
client: 188.170.195.79, server: isli.site,
request: "GET /static/admin/css/base.css HTTP/1.1", host: "isli.site",
referrer: "http://isli.site/admin/login/?next=/admin/"
In nginx settings /etc/nginx/sites-available/forecast static files location is as follow
location /static/ {
root /home/isli/projectsforecast;
}
According to nginx logs
"/usr/share/nginx/home/isli/projects/forecast/static/static/admin/css/base.css"
in path to static files inserted static twice and i can't catch where this insertion occurs

how to add new django app to deployed django project (using nginx,gunicorn)?

While running the django project locally, I can access my home, admin, app1, app2 directory (i.e
localhost:portnum , localhost:portnum/admin ,
localhost:portnum/app1 , localhost:portnum/app2 )
The problem begins when I deployed the app in a server ( I used nginx and gunicorn for django deployment with the help of this guide )
Problem : -
I'm unable able to access example.com/admin, example.com/app1 , example.com/app2.
I'm able to access my home example.com anyway.
When I trying to access example.com/app1/ the page give an error 403 forbidden
2018/11/17 18:00:55 [error] 28459#28459: *8 directory index of "/home/ubuntu/project/app/" is forbidden, client: 172.68.146.88, server: example.com, request: "GET /events/ HTTP/1.1", host: "www.example.com"
2018/11/17 18:00:58 [error] 28459#28459: *13 open() "/usr/share/nginx/html/app" failed (2: No such file or directory), client: 172.68.146.10, server: example.com, request: "GET /events HTTP/1.1", host: "www.example.com"
Some solutions which I tried to follow before this question::-
Django: when trying domain.com/admin gives 404 with an old unexisting urls file
Nginx 403 error: directory index of [folder] is forbidden
My nginx config
server {
listen 80;
listen 443;
ssl on;
ssl_certificate /home/ubuntu/certs/cert.pem;
ssl_certificate_key /home/ubuntu/certs/cert.key;
server_name example.com;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /static/ {
root /home/ubuntu/example_project/app1;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
}
}
Thank You for trying to solve my problem.
When you use= in a location directive, it only applies for that exact path. Instead you should remove those for both of your locations and let nginx match for all prefixes.
location /static/ {
root /home/ubuntu/example_project/app1;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/example_project/exampl_project.sock;
}

Nginx doesn't serve static files

I've just deployed my django website on an Ubunto server. But it doesn't load any static file.
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),]
STATIC_ROOT = 'staticfiles'
nginx/sites-available configuration:
server {
server_name 138.197.172.33;
access_log off;
location /staticfiles/ {
root staticfiles;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
Errors:
...
[14/May/2018 05:28:28] "GET /static/vendors/underscore/underscore.js
HTTP/1.1" 404 113
[2018-05-14 05:28:29,252] base: WARNING - Not Found:
/static/vendors/bootstrap-notify-simple-wrapper/bootstrap-notify-
simple-wrapper.min.js
[14/May/2018 05:28:29] "GET /static/vendors/bootstrap-notify-simple-
wrapper/bootstrap-notify-simple-wrapper.min.js HTTP/1.1" 404 159
[2018-05-14 05:28:29,690] base: WARNING - Not Found:
/static/js/custom.js
[14/May/2018 05:28:29] "GET /static/js/custom.js HTTP/1.1" 404 93
[2018-05-14 05:28:30,236] base: WARNING - Not Found:
/static/js/converter.js
[14/May/2018 05:28:30] "GET /static/js/converter.js HTTP/1.1" 404 96
...
staticfiles folder includes:
admin ajaximage css datatable dm2bn fonts images js static
tinymce vendors
Looking at the error logs, the link is pointing to /static/:
[14/May/2018 05:28:28] "GET /static/ vendors/underscore/underscore.js
HTTP/1.1" 404 113
In your configuration, you have set location to /staticfiles, but your django configuration is saying the URL is /static.
So, location should be /static/, not /staticfiles/.
location is the URL, and the root is the full path to the staticfiles directory.
you should change nginx configuration like
location /static/ {
alias /path/to/staticfiles;
}
You should give absolute path in your nginx configuration.
Make sure all your static files are present in path /static of your project directory.
Once you deploy your app on the server don't forget to run :
python manage.py collectstatic --link
--link will create a symbolic link to each file into the directory /staticfiles.
Nginx config should be :
location /static/ {
alias /path/to/staticfiles;
}

Issues serving static files with nginx (Django)

I'm a bit new to this but I am trying to deploy a website I build using Django to DigitalOcean using nginx/gunicorn.
My nginx file looks as so:
server {
listen 80;
server_name xxx.xxx.xxx.xx;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static/ {
alias ~/dev/WebPortfolio/static/;
}
}
And my settings.py file looks as so:
STATIC_ROOT = '~/dev/WebPortfolio/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
Every time I run python managy.py collect static the errors look as so:
You have requested to collect static files at the destination
location as specified in your settings:
/root/dev/WebPortfolio/~/dev/WebPortfolio/static
Looking at the nginx error log I see (cut out the repetitive stuff):
2015/10/08 15:12:42 [error] 23072#0: *19 open() "/usr/share/nginx/~/dev/WebPortfolio/static/http:/cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.e asing.min.js" failed (2: No such file or directory), client: xxxxxxxxxxxxxx, server: xxxxxxxxxxxxxx, request: "GET /static/http%3A//cdnjs.cloudflare.com/aj ax/libs/jquery-easing/1.3/jquery.easing.min.js HTTP/1.1", host: "XXXXXXXX.com", referrer: "http://XXXXXXXX.com/"
2015/10/08 15:14:28 [error] 23072#0: *24 connect() failed (111: Connection refused) while connecting to upstream, client: xxxxxxxx, server: 104.236.174.46, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "xxxxxxxx.com"
1) I'm not entirely sure why my destination for static files is '/root/dev/WebPortfolio/~/dev/WebPortfolio/static'
Because you've used '~' in a path. That's a shell thing, not a general path thing, and unless you tell Python specifically, it won't know what to do with it. Use a full absolute path in both Django settings and nginx.

sprockets failed to prepare assets for nginx

I want to let nginx serve the static files for cache task.
But I got No such file or directory from nginx error log.
It seems the css files included in root-c452663d516929cd4bb4c1cd521971eb.css could not be served.
How could I fix the bug
production.rb
# Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this.
#config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.serve_static_files = false
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css
/* Summernote */
#import url('plugin/summernote/summernote.css');
#import url('plugin/summernote/summernote-bs3.css');
/* Sweet Alert */
#import url('plugin/sweet-alert/sweet-alert.css');
/* Data Tables */
#import url('plugin/datatables/datatables.css');
/* Chartist */
#import url('plugin/chartist/chartist.min.css');
/* Rickshaw */
#import url('plugin/rickshaw/rickshaw.css');
#import url('plugin/rickshaw/detail.css');
#import url('plugin/rickshaw/graph.css');
#import url('plugin/rickshaw/legend.css');
nginx setting
location / {
try_files $uri #sample;
gzip_static on;
expires max;
add_header Cache-Control public;
}
location #sample {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://sample;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
Error log
2015/07/22 17:27:02 [error] 9891#0: *9 open() "/var/public/assets/kode/css/plugin/date-range-picker/daterangepicker-bs3.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/date-range-picker/daterangepicker-bs3.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
2015/07/22 17:27:02 [error] 9891#0: *10 open() "/var/public/assets/kode/css/plugin/rickshaw/legend.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/rickshaw/legend.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
2015/07/22 17:27:02 [error] 9891#0: *11 open() "/var/public/assets/kode/css/plugin/rickshaw/detail.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/rickshaw/detail.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
2015/07/22 17:27:02 [error] 9891#0: *5 open() "/var/public/assets/kode/css/plugin/fullcalendar/fullcalendar.css" failed (2: No such file or directory), client: 118.166.217.131, server: www.localhost, request: "GET /assets/kode/css/plugin/fullcalendar/fullcalendar.css HTTP/1.1", host: "localhost", referrer: "http://localhost/assets/kode/css/root-c452663d516929cd4bb4c1cd521971eb.css"
puma.config
app_path = File.expand_path('../', File.dirname(__FILE__))
pidfile "#{app_path}/tmp/pids/puma.pid"
bind "unix:///tmp/puma.lazyair.sock"
stdout_redirect "#{app_path}/log/puma.stdout.log", "#{app_path}/log/puma.stderr.log", true
workers Integer(ENV['WEB_CONCURRENCY'] || 6)
threads_count = Integer(6)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3457
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
on_worker_boot do
ActiveRecord::Base.establish_connection
end
activate_control_app