I try to deploy my Django project in a remote server using Nginx but my files are not served.
I guess my path is incorrect but don't really know why...
python3 manage.py collectstatic get all my files in intensecov_static folder.
/home/zebra/
- intensecov_app
- intensecov
- coverage (project)
- manage.py
- static
- ...
- intensecov_static
- css
- style.css
- images
- ...
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'static'),
os.path.join(BASE_DIR,'randomization_management/static'),
os.path.join(BASE_DIR,'randomization_settings/static'),
os.path.join(BASE_DIR,'randomization/static'),
)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = '/home/zebra/intensecov_static'
/etc/nginx/sites-available/intensecov
server {
listen 80;
server_name 192.168.80.9;
root /home/zebra/intensecov_app;
location /static {
alias /home/zebra/intensecov_static;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
break;
}
}
}
Wishing your nginx.conf you declared:
location /static {
alias /home/zebra/intensecov_static;
}
Try this:
location /static {
alias /home/zebra/intensecov_static/;
}
It seems nginx needs the trailing / to make the folder work.
Or at least that the serving of static-files broke when removing the trailing /.
STATIC_ROOT is where all the static files are collected when you run the command python manage.py collectstatic
and STATIC_URL is what points to the STATIC_ROOT.
Check here for reference https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-STATIC_ROOT
Changing the location in nginx file should solve the porblem.
Related
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/;
}
I currently have an multi-docker container application (nginx, postgres RDS, Django) running on Elastic BeanStalk and I am able to use it but the static files (CSS files and JS scripts) are not loaded. This is my current configuration:
nginx setup file
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
client_max_body_size 100M;
server {
listen 80;
charset utf-8;
server_name mydashboard.com;
access_log /dev/stdout;
error_log /dev/stdout info;
location /media/ {
alias /var/www/media/;
}
location /static/ {
alias /var/www/static/;
}
location / {
proxy_pass http://web:8888;
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;
}
}
}
.ebextensions (folder)
django.config (file)
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "mydashboard.settings"
"ALLOWED_HOSTS": ".elasticbeanstalk.com"
"aws:elasticbeanstalk:container:python":
WSGIPath: mydashboard/mydashboard/wsgi.py
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "www/static/"
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR,'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, "..", "www", "static")
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
If I remove the folder .ebextensions folder and load the app, it will work without displaying the static files but if I add the folder with the django.conf file the app won't deploy and I will encounter the error: Invalid option specification (Namespace: 'aws:elasticbeanstalk:container:python:staticfiles', OptionName: '/static/'): Unknown configuration setting.
In one post I found (Serving static files in Django) it is mentioned that all staticfiles directives from .config files should be removed and under the Software Configuration I should configure the static files under the Static File section, however, this Static File section is not even displayed. What code am I missing for displaying the static files? Thanks in advance for your suggestions and answers.
There is new settings for EBS
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: mysite.wsgi:application
aws:elasticbeanstalk:environment:proxy:staticfiles:
/static: static
container_commands:
01_collectstatic:
command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py collectstatic --noinput"
02_migrate:
command: "source /var/app/venv/staging-LQM1lest/bin/activate && python manage.py migrate --noinput"
leader_only: true
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
I have a live server running django, the address is http://179.188.3.54/ . As you can see, the app works but looks like the static files arent working. Also if I click in any other link, doesnt work.
This website is running without any problems in development version. Im not sure what I should do to fix this problem.
Here is my nginx config file and my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/cinegloria/cinegloria/cinegloria/static/'
PS: I tried to run collectstatic ;)
server {
root /usr/share/nginx/www;
index index.html index.htm;
access_log /var/log/nginx/domain-access.log;
server_name 0.0.0.0;
location / {
try_files $uri $uri/ /index.html;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://0.0.0.0:8000/;
}
}
Any ideas or sample code will be appreciated!
Add the static serving to the nginx conf before the / pattern:
location /static {
alias /cinegloria/cinegloria/cinegloria/static/;
}
location / {
...
}
Or set the STATIC_ROOT to the directory under the www root:
STATIC_ROOT = '/usr/share/nginx/www/static'
Or add the symlink from www root to you static dir:
$ ln -s /cinegloria/cinegloria/cinegloria/static /usr/share/nginx/www/static
Add another nginx directive for the static files. Static files should be served by nginx, not the Django server.
location /static/ {
alias /cinegloria/cinegloria/cinegloria/static/;
}
If that still doesn't work, you may need to add the mime type directive. I had to do that yesterday, because for some reason nginx wasn't serving the correct mime type when I used an alias.
As a helpful pointer, whenever you run into problems like this, take a look at your nginx error log and paste the last few lines for debugging. It is located at /var/log/nginx/error.log or a similar path.
Right now I'm working on Django project and this is my first time with running Django app on server (Lighttpd) via gunicorn. The problem is - I run application with gunicorn like this:
$PYTHON $MANAGEPATH run_gunicorn -D -b 127.0.0.1:18002 -p $GUNICORN_PIDPATH --log-file $GUNICORN_LOGPATH
Part of Django settings.py:
from __future__ import absolute_import
import os
from socket import gethostname
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
HOSTNAME = gethostname()
def make_path(path):
return os.path.join(BASE_DIR, path)
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = make_path('static_root')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
In /etc/lighttpd/vhost.d/my_conf.conf I have nice redirect for non-static files:
$SERVER["socket"] == ":82" {
server.document-root = "/home/mefioo/public_html/generator/static"
$HTTP["url"] !~ "\.(js|css|gif|jpg|png|ico|txt|swf|html|htm|svg|xlsx)$" {
proxy.server = ("" => ( "localhost" => (
"host" => "127.0.0.1",
"port" => 18002,
"fix-redirects" => 1
) ) )
}
}
Idea - static files like .js or .css are in
/home/mefioo/public_html/generator/static
(which is proxy to static_root dir in Django app) and should be avaiable from url like
my_domain.com:82/my_file.js
Rest of the app, with all the urls like this:
my_domain.com:82/url
should look for django app like that:
127.0.0.1:18002/url
because of lighttpd conf.
The problem is - when I try to reach my static files, I got 404, because app is looking for
localhost:8080/my_file.js
instead of
my_domain.com:82/my_file.js
I don't understand why, especially when I have two instances of that app, one on my own server (second PC, doesn't work) and one on external VPS (works as intended). Am I running gunicorn wrong way, or missing something in lighttpd settings?
I am new to django/gunicorn too but here is how I configured my app:
settings in settings.py
STATIC_ROOT = "/path to static files"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
I'm not using lighttpd but nginx and here is the configuration:
server{
server_name example.com www.example.com;
access_log off;
location /static {
autoindex on;
alias /path_to_static_files;
}
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;
proxy_set_header Host $host;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
After that we have we have to collect the static files in the configured directory. This is done with: python3 manage.py collectstatic
All files should be copied in the correct directory and lightppd or nginx can serve them whereas gunicorn do the django/python - part