nginx not serving django admin panel static files

Hi,
I am able to serve the static files created by be using nginx, gunicorn and docker in production mode, but when I try to access the django admin panel in production mode, the page is accessible but the static files aren’t served .
In the admin panel page (not serving the static file) when I give my username and password, I am getting the Forbidden(403) error . CSRF verification failed. Request aborted.
I have given every permission to the static root directory .

Any help is greatly appreciated.

Thanks in Advance,
Anish Tulsyan .

Even if you have disabled CSRF protection in the rest of your application, it is always applied to the admin.

Please post all settings from your settings file starting with “CSRF_”.

Also, are you using the Django built-in admin login? Or have you created a custom login template? (Have you made any changes that would affect the admin login process?)

In my settings.py file there is no code starting with CSRF_
However the changed code are as follows:
‘’’
SECRET_KEY = config(‘SECRET_KEY’)
DEBUG = config(‘DEBUG’,default=False,cast=bool)
ALLOWED_HOSTS = [‘*’]

MIDDLEWARE = [
‘whitenoise.middleware.WhiteNoiseMiddleware’,
]
STATIC_URL = ‘/static/’

STATIC_ROOT=os.path.join(BASE_DIR,‘static’)

MEDIA_URL = ‘/media/’

MEDIA_ROOT=os.path.join(BASE_DIR,‘media’)

DEFAULT_AUTO_FIELD = ‘django.db.models.BigAutoField’
‘’’
These are the code which I have added. Apart from that I have my code in Dockerfile, docker-compose.yml and nginx->default.conf

I am using the Django built-in admin login .

I think we’re going to need to see the complete error with the traceback from your gunicorn log.

nginx-1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
nginx-1 | /docker-entrypoint.sh: Configuration complete; ready for start up
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:52 +0000] “GET /admin/ HTTP/1.1” 302 0 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:52 +0000] “GET /admin/login/?next=/admin/ HTTP/1.1” 200 4181 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:52 +0000] “GET /static/admin/css/base.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:52 [error] 29#29: *1 open() “/app/static/admin/css/base.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/base.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 2024/03/12 15:05:52 [error] 29#29: *4 open() “/app/static/admin/css/dark_mode.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/dark_mode.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 2024/03/12 15:05:52 [error] 29#29: *5 open() “/app/static/admin/css/nav_sidebar.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/nav_sidebar.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:52 +0000] “GET /static/admin/css/dark_mode.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:52 +0000] “GET /static/admin/css/nav_sidebar.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:53 +0000] “GET /static/admin/css/login.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:53 [error] 29#29: *5 open() “/app/static/admin/css/login.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/login.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:53 +0000] “GET /static/admin/css/responsive.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:53 [error] 29#29: *4 open() “/app/static/admin/css/responsive.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/responsive.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 2024/03/12 15:05:53 [error] 29#29: *1 open() “/app/static/admin/js/theme.js” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/js/theme.js HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:53 +0000] “GET /static/admin/js/theme.js HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:53 +0000] “GET /static/admin/js/nav_sidebar.js HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:53 [error] 29#29: *1 open() “/app/static/admin/js/nav_sidebar.js” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/js/nav_sidebar.js HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:54 +0000] “GET / HTTP/1.1” 200 1971 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:54 +0000] “GET /favicon.ico HTTP/1.1” 404 179 “http://0.0.0.0/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /admin/ HTTP/1.1” 302 0 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /admin/login/?next=/admin/ HTTP/1.1” 200 4181 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *1 open() “/app/static/admin/css/base.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/base.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/css/base.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *4 open() “/app/static/admin/css/dark_mode.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/dark_mode.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/css/dark_mode.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *5 open() “/app/static/admin/css/nav_sidebar.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/nav_sidebar.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/css/nav_sidebar.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/css/login.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/css/responsive.css HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *5 open() “/app/static/admin/css/login.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/login.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *4 open() “/app/static/admin/css/responsive.css” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/css/responsive.css HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/js/theme.js HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *1 open() “/app/static/admin/js/theme.js” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/js/theme.js HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 2024/03/12 15:05:58 [error] 29#29: *1 open() “/app/static/admin/js/nav_sidebar.js” failed (2: No such file or directory), client: 172.19.0.1, server: , request: “GET /static/admin/js/nav_sidebar.js HTTP/1.1”, host: “0.0.0.0”, referrer: “http://0.0.0.0/admin/login/?next=/admin/
nginx-1 | 172.19.0.1 - - [12/Mar/2024:15:05:58 +0000] “GET /static/admin/js/nav_sidebar.js HTTP/1.1” 404 555 “http://0.0.0.0/admin/login/?next=/admin/” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36” “-”

These are the logs which I got from nginx/error.log file

2024/03/12 15:46:29 [emerg] 6244#6244: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 15:46:29 [emerg] 6244#6244: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 15:46:29 [emerg] 6244#6244: still could not bind()
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 15:47:42 [emerg] 6468#6468: still could not bind()
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to 0.0.0.0:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: bind() to [::]:80 failed (98: Unknown error)
2024/03/12 18:29:58 [emerg] 9725#9725: still could not bind()
2024/03/12 21:01:10 [emerg] 9843#9843: open() “/etc/nginx/sites-enabled/django” failed (2: No such file or directory) in /etc/nginx/nginx.conf:60
2024/03/12 21:02:15 [emerg] 10001#10001: open() “/etc/nginx/sites-enabled/django” failed (2: No such file or directory) in /etc/nginx/nginx.conf:60
2024/03/12 21:02:50 [emerg] 10011#10011: open() “/etc/nginx/sites-enabled/django” failed (2: No such file or directory) in /etc/nginx/nginx.conf:60
2024/03/12 21:03:41 [emerg] 10041#10041: open() “/etc/nginx/sites-enabled/django” failed (2: No such file or directory) in /etc/nginx/nginx.conf:60

Regarding the CSRF error, we’d still need to see the gunicorn logs.

Regarding the 404 for the static files, we’ll need to see the docker-compose.yml and the Dockerfile for the Django application, unless you can verify all of the following.

Briefly, there needs to be a data volume that is shared between the nginx and Django containers. The collectstatic command needs to be used to copy all static files to that shared volume. And, nginx needs to be configured to share the static files from that volume.

Side note: When posting code, you need to use the backtick - ` character to fence off the preformatted text, not the apostrophe - '.

Hi ,
I think I localised the issue:
The issue is when I execute the command docker compose up --build, it collects the user defined static files, but it doesn’t collects the admin static files.
I just checked it in the docker image section of vs code .
That’s why I am getting the 404 error when accessing the css files of admin .
Although static files are collected using collectstatic and it shows user defined static files perfectly in the directory structure, but when I run the docker-compose it fails to collect the static files of django admin .

Now my concern is how do I make docker compose to collect static files of admin .

Regards,
Anish .

They should be collected automatically if you have the admin specified in your INSTALLED_APPS setting, and if your STATIC_ROOT setting is correct, and if your collectstatic is collecting the files to a location that will be used by nginx.

One of the issues that may be hiding a deeper issue is using Whitenoise. It’s typically not needed if you’re using nginx to serve static files and if they’re being collected correctly.

INSTALLED_APPS = [
#‘whitenoise.runserver_nostatic’,
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘HomeApp’,
]

STATIC_URL = ‘/static/’
STATIC_ROOT=os.path.join(BASE_DIR,‘static’)

collectstatic is collecting all the files in the static folder correctly, that’s why I was able to get my home.html page with its css

Using whitenoise I was able to get the css of my home.html,otherwise the css of home.html was also not working

Can there be some problem from the docker side ?

You are mixing two different methodologies here, Whitenoise and nginx.

If you’re using nginx to serve your static files, you should not be using Whitenoise. If you’re using Whitenoise, then nginx is potentially confusing the issue.

You should decide what you’re going to use, and then configure your environment appropriately.

Sir,

The issue is solved. It was the docker version issue.
I uninstalled docker 25 and installed docker 23 and the problem is solved.
In docker version 25 docker compose up --build was not collecting the static files of the admin,but docker 23 has collected the static files of the admin and the problem is solved.

Thanks a lot for your time and effort.
Regards,
Anish

You may have it “working” now, but if you don’t address the Whitenoise / Nginx issue, you’re leaving yourself open to other issues down the road.

Hello Sir,
I commented out the code for whitenoise from my settings.py file and requirements.txt , and my code is working as expected . It was actually docker version issue .

Many Many thanks for your valuable input .

Regards,
Anish .