Optimizing Mendix for low bandwidth - Using Compression

12
Mendix writes out gzipped versions for resources under the ./deployment/web folder - that is awesome. We want the browser to download mendix.jz.gz - this will save lots of bandwidth. However - how do you get the browser to use these? I have tried the following 2 strategies to optimize download and traffic compression. 1) Force requests to use the gripped versions of resources if they are available 2) Dynamically compress the rest. The Dynamic compression works ok. However, it seems that most of the time the mendix.js and dojo.js are not compressed. The bigger problem is that I can not get the gzipped resources to work with the Apache configuration file below. Please have a look and see where my error is - OR - what should one do to enable the use of the gzipped versions of the resources under /web. Here is my Mendix.conf for Apache 2.x with the static and dynamic compression: <VirtualHost *:80> ServerName hjonck.local ErrorLog /var/log/apache2/hjonck-error.log CustomLog /var/log/apache2/hjonck-access.log common DocumentRoot "/Users/hjonck/Development/DA/ApacheHosted/deployment/web" <Location /> order allow,deny allow from all #RedirectMatch ^/.* https://hjonck.local </Location> ProxyRequests Off ProxyVia on Options +FollowSymlinks RewriteEngine on RewriteLog "/var/log/apache2/hjonck-rewrite.log" RewriteLogLevel 5 # Rewrite to gz versions of the files : If there is a gz version - change URL to it RewriteCond %{HTTP_USER_AGENT} !Safari RewriteCond %{REQUEST_FILENAME} !^.+\.gz$ RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b [NC] RewriteCond /Users/hjonck/Development/DA/ApacheHosted/deployment/web%{REQUEST_FILENAME}.gz -s RewriteRule ^(.+) $1.gz [QSA,L,H=Content-Encoding:gzip] # The main Mendix reverse proxy rule for /xas /file /ws - everything is passed that does not have something on file system RewriteCond /Users/hjonck/Development/DA/ApacheHosted/deployment/web%{REQUEST_FILENAME} !-f RewriteRule ^/(.*) http://10.211.55.18:8380/$1 [QSA,P,R,L] ProxyPassReverse / http://10.211.55.18:8380/ # Deflate setup SetOutputFilter DEFLATE DeflateCompressionLevel 9 DeflateFilterNote ratio SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary BrowserMatch \bMSIE !no-gzip !gzip-only-text/html AddType application/javascript *.js AddType text/css *.css # Add all the types we want compressed AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript AddOutputFilterByType DEFLATE text/* AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript #Header append Vary User-Agent DeflateFilterNote Input instream DeflateFilterNote Output outstream DeflateFilterNote Ratio ratio LogFormat '"%r",%b,%{outstream}n/%{instream}n,%{ratio}n%%' deflate CustomLog /var/log/apache2/hjonck-deflate.log deflate SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown </virtualhost>
asked
2 answers
12

I managed to solve it. The key was to use content negotiation and not rewrite rules.

Here is the uber optimized Mendix Apache config file, it has the following features:

  1. Serves up pre-compressed gzip versions of javascript, css, html and mxf files
  2. Dynamically compresses all communication responses from the server from /xas

This results in a typical reduction from about 1,400 KB to around 308 KB!!

Here is the file. (Please let me know if you have a better/shorter way of doing this.) The only known issue is that /file requests do not seem to get the file. I will look into this...

<VirtualHost *:80>
ServerName hjonck.local
ErrorLog /var/log/apache2/hjonck-error.log
CustomLog /var/log/apache2/hjonck-access.log common
DocumentRoot "/Users/hjonck/Development/DA/ApacheHosted/deployment/web"
Options +FollowSymlinks -MultiViews 

<Location />
    order allow,deny
    allow from all
</Location>

<LocationMatch "^/mxclientsystem">
    order allow,deny
    allow from all
    Options +MultiViews 
    AddEncoding gzip .js.gz
    AddType text/javascript .js.gz
    AddEncoding gzip .css.gz
    AddType text/css .css.gz
    AddEncoding gzip .html.gz
    AddType text/html .html.gz
</LocationMatch>

<LocationMatch "^/forms">
    order allow,deny
    allow from all
    Options +MultiViews
    AddEncoding gzip .mxf.gz
    AddType application/xml .mxf.gz
    AcceptPathInfo On
</LocationMatch>

ProxyRequests Off
ProxyVia on
RewriteEngine on    
RewriteLog "/var/log/apache2/hjonck-rewrite.log"
RewriteLogLevel 5
# The main Mendix reverse proxy rule for /xas /file /ws - everything is passed that does not have something on file system
RewriteCond /Users/hjonck/Development/DA/ApacheHosted/deployment/web%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*) http://10.211.55.18:8380/$1 [QSA,P,R,L]
ProxyPassReverse / http://10.211.55.18:8380/

# Deflate setup 
SetOutputFilter DEFLATE
AcceptPathInfo On
DeflateCompressionLevel 9
DeflateFilterNote ratio
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary 
SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# Explicit mime types
AddType application/javascript *.js
AddType text/css *.css
AddType application/xml *.mxf
# Add all the types we want compressed
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript
AddOutputFilterByType DEFLATE text/* 
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
#Header append Vary User-Agent
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio
LogFormat '"%r",%b,%{outstream}n/%{instream}n,%{ratio}n%%' deflate
CustomLog /var/log/apache2/hjonck-deflate.log deflate

SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown

</virtualhost>

answered
8

When using the nginx web server, you can do:

gzip on;
# will gzip json responses (from /xas/) on the fly
gzip_proxied any;
gzip_types application/json;
# Boo!
gzip_disable "msie6";
# will automagically present the .gz files
gzip_static on;

;-)

answered