skip to Main Content

I am experiencing some weird behaviour after switching off DEBUG on Django, behind NGINX reverse proxy.

I am testing it on a Armbian Linux with Python 3.7.3.

Django project has the manage.py inside the folder

folder configuration is

- /var/webserver/backend
  - manage.py
  - backend
    - settings.py
  - static
    - img (contains collected plus my logo and favicon)
    - admin (the collected admin files)

The NGINX configuration is as following (the static part, the rest is a reverse proxy with a self signed certificate):

location /static {
      autoindex on;
      autoindex_exact_size off;
      alias /var/webserver/backend/static;
    }

The settings.py static part is as following, staticfiles app is in the APPS:

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'

I run the manage.py command while in the /var/webserver/backend directory, nginx is ran as service.

I think I have tried all combinations of "slashes" both inside the NGINX configuration and settings.py (before, after, both, none)

This configuration is the nearest to work, the situation is the following:

  • The logo.png and favicon.ico inside the static/img folder are displayed correctly
  • all the css, js or others are not loaded (page is completely without style)
  • There are no 404 in the logs (either Django or NGINX access.log shows 200).
  • If I navigate to the paths I can see in the page inspector (e.g. the <link rel="stylesheet" type="text/css" href="/static/admin/css/base.css"> in the head section) I can see the file (e.g. by navigating to https://address/static/admin/css/base.css)
  • I tried changing permissions and owner to the files, going to a 777 permission and root:www-data as owner
  • I tried installing WhiteNoise with no success.

I kept cache disabled on the browser to avoid non-reloaded pages.

I really can’t get what’s not working.

I tried changing the configuration any way, I tried installing WhiteNoise and put it in the Apps with no success.
I tried changing "alias" with "root" inside the NGINX configuration and it went worse (also images went down)

Thanks anyone who will help, let me know if something else is needed.

2

Answers


  1. There might be a several reasons why it is happening.

    1. nginx alias directive should end with slash (/) [link
      (https://nginx.org/en/docs/http/ngx_http_core_module.html#alias)
      You should add slash after static too.
     location /static/ {
        ...
        alias /var/webserver/backend/static/;
      }
    
    1. Make sure you include mime types in your nginx conf file.

    2. Some people report that Whitenoise’s whitenoise.storage.CompressedManifestStaticFilesStorage storage backend doesn’t work well in some cases. Try to change it to whitenoise.storage.CompressedStaticFilesStorage.

    Login or Signup to reply.
  2. reasons why it not working.

    1. make sure you add MEDIA_ROOT in urls.py

      urlpatterns = [path('', include('app_one.urls')),] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

      if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

    2. add {% load 'static' %} with
      <link rel="stylesheet" href="{% static 'fonts/icomoon/style.css' %}">
      in html file

    3. Run python manage.py collectstatic
      Then it would bundle all static files in project staticfiles OR root
      static_files folder, depending on where you want them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search