Compressing the response payload in Django REST?

前端 未结 3 698
悲&欢浪女
悲&欢浪女 2020-12-11 04:25

I was wondering: would it be possible to compress the response payload in Django REST?

At the moment, the response payloads are plain JSON data. However, there\'s qu

3条回答
  •  甜味超标
    2020-12-11 05:05

    The following worked for me.

    I actually turned gzip on at the nginx level, not within Django or Django Rest Framework.

    /etc/nginx/nginx.conf file:

        http {
    
            #... other settings ...#
    
            ##
            # Gzip Settings
            ##
    
            gzip on;
            gzip_disable "msie6";
    
            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/x-javascript text/xml application/xml application/xml+rss text/javascript;
        }
    

    This leaves the compressing up to the nginx server and as most modern browsers automatically know how to extract (uncompress) gzip compression, I didn't need to do anything on my client-side - even when receiving json data inside an Angular spa app.

    My 1.3 MB JSON payload turned into about a 180 KB payload.

    A pretty quick and fast way to save MB's of data.

提交回复
热议问题