How to enable gzip on proxy servers on Apache

I’m starting to use the gunicorn django app using supervisord. Here my configuration:

  • Varnish: port 80
  • Apache: port 8080
  • gunicorn: port 4180 (/path/to/my/manage.py run_gunicorn localhost:4180)

Only the port 80 is exposed to other clients than localhost. The Varnish default backend is Apache (localhost:8080). I have a Drupal installation and a django installation on the same machine: since I want to expose django on the same domain at a defined location, I add to Apache this location:

ProxyRequests Off
ProxyPreserveHost On

Order deny,allow
Allow from all

# on port 4180 gunicorn is running
# @see /etc/supervisor.conf
ProxyPass /foo http://localhost:4180/
ProxyPassReverse /foo http://localhost:4180/
Order allow,deny
Allow from all
AddOutputFilterByType DEFLATE text/html

You can omit AddOutputFilterByType DEFLATE text/html: here I just take the response from gunicorn, compress and then serve to the client in this way:

(client) -> varnish -> apache -> gunicorn

(client)
                (X-Varnish-Cache: MISS) 

Here an example of what I get:
Image

It’s a big page, but using gzip from 2.2 MB of the uncompressed page I get 417 KB gzipped text/html, less than 1/4 of the original!

Read also on the same topic: How to enable gzip on proxy servers on nginx

Improve Drupal performance

I use YSlow to check a Drupal site performance. Here some tips to get an A with Drupal 5.x.

Step 1: Fewer HTTP requests:

  • Aggregate CSS (/admin/settings/performance)
  • Aggregate JS with Javascript Aggregator (experimental). To avoid JavaScript errors in some modules (i.e. TinyMCE), you can use the dirscard list patch by derjochenmeyer on the dev version (it’ll be added to the next release). To Drupal 6.x users: Drupal 6.x comes with a native JS Aggregator support, so you don’t need an additional module.
  • Reduce CSS background images: select carefully your theme.

Step 2: Compress pages on-the-fly:
Many modern browsers support Gzip compression. Through .htaccess, you can tell your server to serve gzipped HTML pages, JavaScript and CSS files (or any other text file, since images like JPG and PNG are compressed natively). You can use two approach, depending on your server settings, just add on bottom of your .htaccess:

  1. PHP flag method (compress only php served pages):
    # via-PHP compression (only with compatible browsers)
    php_flag zlib.output_compression On
  2. Apache mod_deflate method (preferred method where available)
    # Enable file compression by MIME type
    AddOutputFilterByType DEFLATE text/html text/plain text/xml
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE text/css
    # Exclude Not compatible browsers (uncomment to activate)
    # BrowserMatch ^Mozilla/4 gzip-only-text/html
    # BrowserMatch ^Mozilla/4\.0[678] no-gzip
    # BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

    Using this method, you can compress HTML pages, JavaScript, CSS files specifying ;MIME type

See also / references