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;
}
Related
I have a nginx / gunicorn / django app I've set up following
https://medium.com/#_christopher/deploying-my-django-app-to-a-real-server-part-ii-f0c277c338f4
It works when I go to the main IP from the browser (I got Django tried these URL patterns, in this order: admin/...) but when I go to /admin I got a 404. Nginx logs are as follows:
2019/07/05 00:30:28 [error] 13600#13600: *1 open() "/usr/share/nginx/html/admin" failed (2: No such file or directory), client: 186.190.207.228, server: 127.0.0.1, request: "GET /admin HTTP/1.1", host: "128.199.62.118"
So it is trying to serve files from html/ instead of serving gunicorn, why?
Nginx config:
server {
listen 80;
server_name 127.0.0.1;
location = /favicon.ico {access_log off;log_not_found off;}
location = /static/ {
root /home/juan/site;
}
location = /media/ {
root /home/juan/site;
}
location = / {
include proxy_params;
proxy_pass http://unix:/home/juan/site/site.sock;
}
}
Remove the = from all the Location directives except the first. That means exact match, instead of the prefix match you want.
location = /favicon.ico {access_log off;log_not_found off;}
location /static/ {
root /home/juan/site;
}
location /media/ {
root /home/juan/site;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/juan/site/site.sock;
}
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;
}
I dont know how nginx serves the static files
/etc/nginx/site-available/default :
upstream example.com {
server unix:/home/www/example/env/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
access_log /home/www/example/logs/nginx-access.log;
error_log //home/www/example/logs/nginx-error.log;
location /static/ {
alias /home/www/example/codeab/static/;
}
location /uploads/ {
alias /home/www/example/codeab/uploads/;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/www/example/env/run/gunicorn.sock;
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/www/example/templates/;
}
error_page 404 /401.html;
location = /401.html {
root /home/www/example/codeab/templates/;
internal;
}
}
Django setting.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_content/'),
)
STATIC_ROOT = os.path.join(BASE_DIR,'static/')
Also , I did collectstatic before DEBUG = False
It seems that nginx is not considering the above config file, as for 404 page it is not showing my custom 401.html page.
After editing the config, I linked it to site-enabled by
sudo ln -s /etc/site-available/default /etc/site-enabled/default
service nginx reload
sudo service nginx restart
Please help, or let me know If I am missing any things.
I dont know the exact solution, but it started working on changing my gunicorn binding from xxx.xxx.xx.xx:80 to xxx.xx.xx.xx:8000
and editing config as -
location / {
include proxy_params;
#proxy_pass http://unix:/home/www/example/env/run/gunicorn.sock;
proxy_pass http://xxx.xxx.xx.xx:8000;
}
I commented proxy_pass of sock file and added the new line as you can see above.
Nginx serves static files from the dir defined in nginx config file under location /static/ and redirects to custom 404 html page as defined in nginx config by you when it does not find such static resource in the defined dir.
Django displays the custom 404 html page when it does not find given url in any of the urls.py files of Django, provided you set Debug = False in settings.py and also point the 'TEMPLATES'-'DIRS' setting to the dir containing your custom 404 html file as explained in the Django documentation.
So, effectively the way nginx and Django displays the same 404 html page is different and independent of each other.
Just change your the configuration in the nginx server block from
location /static/ {
alias /home/www/example/codeab/static/;
}
To
location /static/ {
alias /home/www/example/codeab/static_content/;
}
Reason, after the command python manage.py collectstatic has executed
the static files are been placed inside of the static_content folder.
Static file can't serve via nginx. I didn't found any answer for this question so please help me to figure out.
settings.py
DEBUG = False
/etc/nginx/sites-available/example.com
upstream test {
server 127.0.0.1:8000;
keepalive 500;
}
server {
listen 80;
server_name example.com;
client_max_body_size 4G;
location /static/ {
alias /home/user/live/staticfiles/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://test;
}
}
cat /var/log/nginx/error.log shows me below output
2015/01/20 05:43:07 [error] 4480#0: *7 open() "/home/user/live/staticfiles/images/footer-logo.png" failed (13: Permission denied), client: 182.74.206.202, server: example.com, request: "GET /static/images/footer-logo.png HTTP/1.1", host: "example.com", referrer: "example.com/"
Update :
I just say it's not working ... Still getting same error i can't believe this happening because of file permission issue. I included below detail...
[myuserm#instance-4 home]$ namei -om /home/myuser/live/staticfiles/js/jquery.fancybox.js
f: /home/myuser/live/staticfiles/js/jquery.fancybox.js
dr-xr-xr-x root root /
drwxr-xr-x root root home
drwxrwxrwx myuser myuser myuser
drwxrwxrwx root root live
drwxrwxrwx myuser myuser staticfiles
drwxrwxrwx myuser myuser js
-rwxrwxrwx myuser myuser jquery.fancybox.js
First of all you check your setting for static file that should look like this setting
STATIC_ROOT = "/home/user/live/collected_static/"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
PROJECT_DIR.child("static"),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
then you run ./manage.py collectstatic # this will collect all static
files to collected_static folder,
then in your nginx setting :
location /static {
alias /home/user/live/collected_static/;
}
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://test;
break;
}
}
Hope this will resolved your issue. and also check permission should we same as django project code. no need to give any extra permission.
For more setting and a very good doc you look here: Setting up Django with Nginx, Gunicorn, virtualenv, supervisor and PostgreSQL
For nginx permission issue you will find solution from these links:
https://serverfault.com/questions/628394/nginx-is-unable-to-open-and-thus-serve-files-despite-proper-filesystem-permiss
Nginx 403 forbidden for all files
Nginx is throwing an 403 Forbidden on Static Files
I have developed django application and now deploying this on freebsd server using gunicron and nginx. I have added guncorn in projects settings files under installedapps. and placed gunicorn.conf.py in root as:
import os
def numCPUs():
if not hasattr(os, "sysconf"):
raise RuntimeError("No sysconf detected.")
return os.sysconf("SC_NPROCESSORS_ONLN")
user = root
workers = numCPUs() * 2 + 1
bind = "127.0.0.1:8000"
pidfile = "/tmp/gunicorn-srv.pid"
backlog = 2048
logfile = "/data/logs/gunicorn_srv.log"
loglevel = "info"
Then in nginx.conf updated server tag with:
listen 80;
server_name localhost;
access_log /var/log/nginx/nginx-access.log;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://127.0.0.1:8000;
#root /usr/local/www/nginx;
#index index.html index.htm;
}
location /media/ {
root /data/webs/uni_rate;
}
location /static/ {
root /data/webs/uni_rate;
}
Now I have started nginx and guncorn. If I brows my public ip page is shown but static files are not served. If I directly hit
http://myip/static/folder/file.css
then 403 error is returned. Any Idea what could be the reason or what else information should be provided to diagnose it.
My media and static directories are respectively:
/data/webs/uni_rate/media
/data/webs/uni_rate/static
here is an output of nginx-access.log
182.178.6.248 - - [01/Apr/2013:11:14:54 -0500] "GET /static/bootstrap_toolkit_extras/css/bootstrap.css HTTP/1.1" 403 168 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv
:16.0) Gecko/20100101 Firefox/16.0"
182.178.6.248 - - [01/Apr/2013:11:15:00 -0500] "-" 400 0 "-" "-"
Have you checked the read permission of all of the parent folders of /data/webs/uni_rate/? Nginx user has to have read access to all of them. For example, /data, /data/webs, /data/webs/uni_rate.
Try to replace your location for static and media with something like this:
location /static {
alias /var/www/domain1/media/;
autoindex on;
access_log off;
}
The trick is in 'alias' instead of 'root'.