Can't retrieve images from filesystem with Next.js and Django

English is not my native language.

I'm trying to deploy three Next.js apps and one Django API app(using Django ninja). the problem is I get "400 bad request" in my browser's console.

https://MY_SERVER_DOMAIN/users/_next/image?url=https://MY_SERVER_DOMAIN/media/magazine/thumbnail/Methods-of-using-radish-in-diet.webp&w=1920&q=75

in local I can see the image but on production I get 400.

my Nginx config:


        location /media/ {
                alias /home/USER/backend/media/;
        }

        location /api/v1 {
                include proxy_params;
                proxy_pass http://localhost:8000;
        }
        location /admin {
                include proxy_params;
                proxy_pass http://127.0.0.1:8000;
        }

        location / {
                include proxy_params;
                proxy_pass http://localhost:3000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

        location /doctors {
                include proxy_params;
                proxy_pass http://localhost:3001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }

        location /users {
                include proxy_params;
                proxy_pass http://localhost:3002;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

I saw many errors like this this in /var/log/nginx/error.log:

2025/04/06 13:44:21 [error] 41446#41446: *1 directory index of "/home/USER/users-front/.next/static/media//" is forbidden, client: 5.125.0.145, server: 176.97.218.11, request: "GET /users/_next/image/?url=%2Fusers%2F_next%2Fstatic%2Fmedia%2Fdoctor.07a8ef12.jpg&w=96&q=75 HTTP/1.1", host: "MY_SERVER_DOMAIN", referrer: "https://MY_SERVER_DOMAIN/users"

but it should retrieve the image from Django media directory("backend/media") not Next.js!

Back to Top