I ran into a problem while publishing the site. When turned on Debug=False the pictures disappear.
Log nginx error:
2023/03/06 11:17:39 [error] 646224#646224: *15 open() "/home/egor/mysite/PersonalPortfolio-project/staticportfolio/image/django_certificate-1.png" failed (2: No such file or directory), client: 109.198.191.208, server: zyoger.ru, request: "GET /media/portfolio/image/django_certificate-1.png HTTP/1.1", host: "www.zyoger.ru", referrer: "http://www.zyoger.ru/about"
2023/03/06 11:17:39 [error] 646224#646224: *14 open() "/home/egor/mysite/PersonalPortfolio-project/staticportfolio/image/python_certificate-1.png" failed (2: No such file or directory), client: 109.198.191.208, server: zyoger.ru, request: "GET /media/portfolio/image/python_certificate-1.png HTTP/1.1", host: "www.zyoger.ru", referrer: "http://www.zyoger.ru/about"
Settings nginx:
server {
listen 80;
server_name zyoger.ru www.zyoger.ru zyoger.online www.zyoger.online;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/{
autoindex on;
alias /home/egor/mysite/PersonalPortfolio-project/static;
}
location /media/{
autoindex on;
alias /home/egor/mysite/PersonalPortfolio-project/static;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
Settings django:
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I’ve read a lot of articles and I can’t figure it out.
How To Set Up Django with Postgres, Nginx, and Gunicorn on Ubuntu 18.04
>Solution :
Based on the error message, it seems that Nginx is not able to find the static files when Debug is set to False. When Debug is True, Django serves static files, but when it is set to False, Django does not serve static files and it is the responsibility of the web server (Nginx) to serve them.
In your Nginx configuration, it seems that you are pointing to the wrong directory for the static and media files. You are pointing to "/home/egor/mysite/PersonalPortfolio-project/static" for both "location /static/" and "location /media/" blocks. However, based on your Django settings, the static and media files should be served from different directories. Static files should be served from "/home/egor/mysite/PersonalPortfolio-project/static" and media files should be served from "/home/egor/mysite/PersonalPortfolio-project/media".
Therefore, you should update your Nginx configuration as follows:
server {
listen 80;
server_name zyoger.ru www.zyoger.ru zyoger.online www.zyoger.online;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/egor/mysite/PersonalPortfolio-project/static/;
}
location /media/ {
alias /home/egor/mysite/PersonalPortfolio-project/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Make sure to restart Nginx after making the changes to the configuration file:
sudo systemctl restart nginx
Also, make sure that the directories "/home/egor/mysite/PersonalPortfolio-project/static" and "/home/egor/mysite/PersonalPortfolio-project/media" exist and that the static and media files are located in the correct subdirectories within them.