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
Related
I'm trying to serve in production (not development) media files for my Django-project with a Nginx-server. The media-files are not located within the django-folder.
The folder structure is like:
|- django_user
| |- media
| |- Myproject
| |- static
| |- myproject
| |- settings.py
Nginx-Server:
upstream websocket{
server 127.0.0.1:6379;
}
server {
server_name myproject.com;
client_max_body_size 5M;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/django_user/Myproject;
}
location /media/ {
root /home/django_user/media;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/myproject.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/myproject.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location /ws {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_set_header X-Forwarded-Proto $scheme;
}
}
server {
if ($host = myproject.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name myproject.com
return 404; # managed by Certbot
}
Here is a part of my settings.py:
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
MEDIA_ROOT = '/home/django_user/media'
MEDIA_URL = '/media/'
Saving files in the media folder is working. I've set up an image field like this:
myicon = models.ImageField(default="static/images/default_icon.png" , blank=True, upload_to="images/")
I've checked it and the images (e.g. test.png) are correctly saved in /home/django_user/media/images. My problem is that the images in the media-folder cannot be shown on my website (myproject.com/images/test.png), it always shows me:
Not Found
The requested resource was not found on this server.
I've done already "manage.py collectstatic", and static files are loaded correctly. Only the media-files are not found.
I've also tried the following variations of my nginx-server, all without success:
location /media/ {
root /home/django_user;
}
location /media {
root /home/django_user/media/;
}
location /media {
root /home/django_user/;
}
location /media/ {
alias /home/django_user/media/;
}
Can you help me with this problem? What am I missing here or where is my mistake?
Try alias method:
location /media/ {
alias /home/django_user/media/;
}
I think you are trying with the wrong URL.
If you define:
MEDIA_URL = '/media/ and location /media/
Then the URL should be myproject.com/media/images/test.png instead of myproject.com/images/test.png in both scenarios (development and production)
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 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.
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;
}
}
(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.