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
Related
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
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;
}
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
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.
I'm trying to set up a production server that consists of Django + uwsgi + Nginx.
The tutorial I'm following is located here http://www.panta.info/blog/3/how-to-install-and-configure-nginx-uwsgi-and-django-on-ubuntu.html
The production server is working because I can see the admin page when debug is on but when I turn to debug off. It displays the Server Error (500) again. I don't know what to do. Ngnix should be serving the Django request. I'm clueless right now, Can someone kindly help me, please.
my /etc/nginx/sites-available/mysite.com
server {
listen 80;
server_name mysite.com www.mysite.com;
access_log /var/log/nginx/mysite.com_access.log;
error_log /var/log/nginx/mysite.com_error.log;
location / {
uwsgi_pass unix:///tmp/mysite.com.sock;
include uwsgi_params;
}
location /media/ {
alias /home/projects/mysite/media/;
}
location /static/ {
alias /home/projects/mysite/static/;
}
}
my /etc/uwsgi/apps-available/mysite.com.ini
[uwsgi]
vhost = true
plugins = python
socket = /tmp/mysite.com.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/projects/mysite/mysite/wsgi.py
virtualenv = /home/projects/venv
chdir = /home/projects/mysite
touch-reload = /home/projects/mysite/reload
my settings.py
root#localhost:~# cat /home/projects/mysite/mysite/settings.py
# Django settings for my site project.
DEBUG = False
TEMPLATE_DEBUG = DEBUG
min/css/base.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/base.css HTTP/1.1", host: "160.19.332.22"
2013/06/17 14:33:39 [error] 8346#0: *13 open() "/home/projects/mysite/static/admin/css/login.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/login.css HTTP/1.1", host: "174.200.14.200"
2013/06/17 14:33:39 [error] 8346#0: *14 open() "/home/projects/mysite/static/admin/css/base.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/base.css HTTP/1.1", host: "174.200.14.2007", referrer: "http://174.200.14.200/admin/"
2013/06/17 14:33:39 [error] 8346#0: *15 open() "/home/projects/mysite/static/admin/css/login.css" failed (2: No such file or directory), client: 160.19.332.22, server: mysite.com, request: "GET /static/admin/css/login.css HTTP/1.1", host: "174.200.14.200", referrer: "http://174.200.14.200/admin/"
I think it's your ALLOWED_HOSTS setting (new in Django 1.5)
Try the following in your settings.py
ALLOWED_HOSTS = ['*']
This will allow everything to connect until you get your domain name sorted.
It's worth saying that when you do get a domain name sorted make sure you update this value (list of allowed domain names). As the documentation for ALLOWED_HOSTS states:
This is a security measure to prevent an attacker from poisoning
caches and password reset emails with links to malicious hosts by
submitting requests with a fake HTTP Host header, which is possible
even under many seemingly-safe webserver configurations.
Also (a little aside) - I don't know if you have a different setup for your django settings per environment but this is what I do:
At the end of your settings.py include:
try:
from local_settings import *
except ImportError:
pass
Then in the same directory as settings.py create a local_settings.py file (and a __init__.py file if using a different structure than the initial template) and set your settings per environment there. Also exclude local_settings.py from your version control system.
e.g. I have DEBUG=False in my settings.py (for a secure default) but can override with DEBUG=True in my development local settings.
I also keep all my database info in my local settings file so it's not in version control.
Just a little info if you didn't know it already :-)
I had the same issue but in my case it turned out to be that STATICFILES_STORAGE was incorrectly set as:
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'
This question has already an accepted answer but I'm leaving this in case someone gets here in the same situation. You can also see this similar answer for the same error.