evansd / whitenoise

Radically simplified static file serving for Python web apps
https://whitenoise.readthedocs.io
MIT License
2.52k stars 148 forks source link

Documentation pointer for nginx users #503

Closed chriswedgwood closed 1 year ago

chriswedgwood commented 1 year ago

Python Version

3.9

Django Version

4.1

Package Version

No response

Description

I was working trying to get compression with brotli working to improve a sites performance. My setup is a digitalocean droplet using gunicorn with nginx proxy. I incorrectly assumed that the .br files were being served until I looked on the network tab at my css file and saw that I was not receiving Content-Encoding: br

I set up https://github.com/google/ngx_brotli and changed my nginx config file to serve brotli_static to true and it worked

I think it woulkd be useful to have something in the documentation that says something like if using nginx to serve static files statically you will need to configure it to use brotli

Also I think it would be helpful to add some information about looking at the network tab and checking content-encoding value

I think a lot of users may be using whitenoise like I do but are not getting the benefits of compression

I'll do the work here but want to get a thumbs/thoughts up here before I do

Thanks Chris

adamchainz commented 1 year ago

I'm a bit confused by your request. It sounds like you're not using whitenoise in the case that you need to reconfigure nginx to support brotli? Whitenoise does the brotli itself normally.

chriswedgwood commented 1 year ago

Might just me being a dummy :)

Ive successfully run collectstatic and the .br files are there so that part works. 👍

Here is my nginx conf for my site which is looking in the collect_static folder for the files. By default nginx does not serve the .br file and I had to install ngx_brotli and set brotli_static to true to use the br files in the collect_static folder.

Am I doing it all wrong?


    server unix:{{app_dir}}/run/gunicorn.sock fail_timeout=0; 
}

server {
  server_name  {{ site_domain }} www.{{ site_domain }} {{ site_domain}};

  location /static/ {
         alias {{ app_dir }}/collect_static/;
    }

  location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-Proto https;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }  

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/{{ site_domain }}/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/{{ site_domain }}/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 = {{ site_domain }}) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = {{ site_domain }}) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

  listen 80;
  server_name  {{ ip_address }} {{ site_domain }} www.{{ site_domain }} {{ site_domain }};
    return 404; # managed by Certbot
}```
adamchainz commented 1 year ago

To be blunt, yes. Nginx should not have any static file configuration. All requests should be sent through to Django, which serves the static files for you.

Try deleting all your static file stuff and it should work, with no nginx brotli support required.