Skip to content

Instantly share code, notes, and snippets.

@christianseel
Last active March 31, 2016 20:32
Show Gist options
  • Save christianseel/4744662 to your computer and use it in GitHub Desktop.
Save christianseel/4744662 to your computer and use it in GitHub Desktop.
# x-ua-xompatible # expire headers for assets # www.mydomain.com -> mydomain.com # filename-based cache busting # trailing slash redirect # statcache rewrite # modx furls rewrite
# x-ua-xompatible header
add_header X-UA-Compatible IE=edge,chrome=1;
#default charset uft-8
charset utf-8;
# allowed domains
set $rewrite_var 0;
if ($host = 'mydomain.com') {
set $rewrite_var 1;
}
if ($host = 'static.mydomain.com') {
set $rewrite_var 1;
}
# rewrite not allowed domains to default
if ($rewrite_var = 0) {
rewrite ^/(.*)$ http://mydomain.com/$1 permanent;
}
# rewrite static domain to assets folder
if ($host = "static.chsmedien.com") {
rewrite ^ /assets$uri last;
}
# expire headers for assets
# 1: images
location ~* ^(/assets/img/[^\.]+)(\.(?:jpe?g|png|gif))$ {
expires 365d;
# Serves static files in high resolution only if required
# Naming convention for high resolution images:
# [filename]@2x[extension], e.g.:
# example@2x.png
set $hidpi_uri $1@2x$2;
if ($http_cookie !~ 'device-pixel-ratio=2') {
# If the device-pixel-ratio cookie is not set to 2, fall back to
# default behaviour, i.e. don't try to serve high resolution image
break;
}
# device-pixel-ratio cookie is set to 2
# Serve high resolution image if available,
# otherwise fall back to standard resolution
try_files $hidpi_uri $uri =404;
}
# 2: js/css/webfonts
# with built-in filename-based cache busting
location ~ ^/assets/(\w+)/(.+)\.(\d+)\.(js|css|woff|eot|ttf|svg)$ {
rewrite ^/assets/(\w+)/(.+)\.(\d+)\.(js|css|woff|eot|ttf|svg)$ /assets/$1/$2.$4;
add_header Vary Accept-Encoding;
expires max;
break;
}
# without filename-based cache busting
location ~* \.(?:js|css)$ {
add_header Vary Accept-Encoding;
expires 12h;
}
location ~* \.(?:woff|eot|ttf|svg)$ {
expires 30d;
}
# modx furls - eigher via statcache or via default modx rewrite
# statcache rewrite - http://modx.com/extras/package/statcache
location / {
try_files /core/cache/statcache$uri~index.html /core/cache/statcache$uri $uri $uri/ @modx-rewrite;
}
# default modx furls rewrite
location / {
try_files $uri $uri/ @modx-rewrite;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment