django+gunicorn+nginx gives 404 while serving static files - django

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/;
}

Related

CSS files loaded but not rendered Django Rest Framework

I have a Django app with Gunicorn and Nginx deployed on AWS ECS. NGINX can load static files but the page still shows only text.
Nginx:
server {
listen 80;
listen [::]:80;
server_name api.example.com;
location /health {
access_log off;
return 200;
}
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
# Main app
location / {
if ($request_method !~ ^(GET|POST|HEAD|OPTIONS|PUT|DELETE|PATCH)$) {
return 405;
}
include /etc/nginx/mime.types;
proxy_pass http://example-api:8000;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header X-Forwarded-Port $http_x_forwarded_port;
}
location /static/ {
autoindex on;
alias /my_app/static/;
}
}
In settings.py
STATIC_URL = '/static/'
STATIC_ROOT = '/my_app/static/'
When I inspect the webpage, all CSS files are loaded, no 404 (It was an issue before but I fixed that). So not sure what else I am missing here.
The UI works fine when running without Nginx and runserver, but on AWS ECS, I use gunicorn.

Serve media files from outside the Django-project-folder with Nginx

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)

Nginx unable to load static files from django

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;
}
}

Can't serve statics with Nginx on DigitalOcean VPS for Django

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

django+gunicorn+nginx 404 serving static files

I have django+gunicorn+nginx running on 192.168.1.81:3000. The web app will not serve any static files; it returns a 404 error. This suggests that there is a problem with the nginx virtual server config file. I have tried several solutions offered on stack overflow with no success. What is wrong with the nginx virtual server file?
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
location /static/ {
alias /home/pi/door_site/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://app_server;
break;
}
}
}
In Nginx virtual server conf file inside server section you need to add to location sections for Nginx can serve files in /static/ and /media/ folders:
location /media {
alias /path/to/your/folder/media;
}
location /static {
alias /path/to/your/folder/static;
}
After that test Nginx configuration:
sudo nginx -t
and reload Nginx:
sudo nginx -s reload
(or restart - sudo /etc/init.d/nginx restart )
try this config using server root and try_files
upstream app_server {
server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name 192.168.1.81;
client_max_body_size 4G;
access_log /home/pi/door_site/logs/nginx-access.log;
error_log /home/pi/door_site/logs/nginx-error.log;
root /path/to/root # place your static directories in root i.e /path/to/root/static
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
This will try to find your static file and then move onto your app server.
Make sure nginx is running as the right user to access your files and that your static files permissions are correctly set perhaps with:
chmod -R u=rwX,g=rwX,o=rX static_dir
I my case it was a permission issue on static directory and it worked after assigning proper permissions.