0

I have set up a django website that would be served by Nginx, everything was working perfectly not until images stopped showing recently.

I tried inspecting the possible cause of this strange development using curl and then realized that the Content-Type is not recognized as Content-Type: image/jpeg returns a Content-Type: text/html; charset=utf-8

Below are my files :

Variables in Settings.py

STATIC_URL = '/home/ubuntu/static/'
MEDIA_URL = "/home/ubuntu/media/"
STATIC_ROOT = '/home/ubuntu/static'
MEDIA_ROOT = '/home/ubuntu/media/'
ONNGO_DOMAIN = "https://onngo.co.in"
PROFILE_PICTURE_URL = MEDIA_ROOT+"profile_pictures/"
STATICFILES_DIRS = (
    os.path.join(CORE_DIR, 'apps/static'),
)

Here is my full nginx.conf file

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;
        types_hash_max_size 2048;
        # server_tokens off;

        server_names_hash_bucket_size 512;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;
        
        ##
        # Gzip Settings
        ##

        gzip on;

        #gzip_vary on;
        #gzip_proxied any;
        #gzip_comp_level 6;
        #gzip_buffers 16 8k;
        #gzip_http_version 1.1;
        #gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/core;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

Contents of /etc/nginx/sites-enabled/core

server{
        server_name onngo.co.in;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                alias /home/ubuntu/static;
        }
        location /ubuntu/media/ {
                alias /home/ubuntu/media;
        }
        location / {
                include proxy_params;
                proxy_read_timeout 300s;
                proxy_connect_timeout 75s;
                proxy_pass http://unix:/run/gunicorn.sock;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/onngo.co.in/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/onngo.co.in/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

}
server{
    server_name www.onngo.co.in; # managed by Certbot


        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
                alias /home/ubuntu/static;
        }
        location /media/ {
                autoindex on;
                alias /home/ubuntu/media;
        }
        location / {
                include proxy_params;
                proxy_read_timeout 300s;
                proxy_connect_timeout 75s;
                proxy_pass http://unix:/run/gunicorn.sock;
        }

      listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/onngo.co.in/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/onngo.co.in/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



}server{
    if ($host = onngo.co.in) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 default_server;
        server_name onngo.co.in;
    return 404; # managed by Certbot


}server{
    if ($host = www.onngo.co.in) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80 ;
    server_name www.onngo.co.in;
    return 404; # managed by Certbot


}

I am running nginx on ec2 instance with postgresQL database on AWS RDS. The problem gets solved when i run my server with Debug = True, but continues to persist when Debug=False.

  • STATIC_URL and MEDIA_URL are both wrong - they are supposed to be "web URLs", not directory paths. – Ivan Starostin Feb 23 '23 at 15:33
  • Does this answer your question? [nginx does not seeing static files, when launching with docker compose +gunicorn](https://stackoverflow.com/questions/75365356/nginx-does-not-seeing-static-files-when-launching-with-docker-compose-gunicorn) – Ivan Starostin Feb 23 '23 at 15:33

0 Answers0