Skip to content

Instantly share code, notes, and snippets.

@bradleyboy
Last active March 22, 2022 19:46
Show Gist options
  • Save bradleyboy/26ffd2ec7da68919ecd1 to your computer and use it in GitHub Desktop.
Save bradleyboy/26ffd2ec7da68919ecd1 to your computer and use it in GitHub Desktop.
nginx rewrite setup for Koken
# Enable gzip. Highly recommending for best peformance
gzip on;
gzip_comp_level 6;
gzip_types text/html text/css text/javascript application/json application/javascript application/x-javascript;
# By default, do not set expire headers
expires 0;
# Set expires header for console CSS and JS.
# These files are timestamped with each new release, so it is safe to cache them agressively.
location ~ "console_.*\.(js|css)$" {
expires max;
}
# Catch image requests and pass them back to PHP if a cache does not yet exist
location ~ "^/storage/cache/images(/(([0-9]{3}/[0-9]{3})|custom)/.*)$" {
# Cached images have timestamps in the URL, so it is safe to set
# aggresive cache headers here.
expires max;
try_files $uri /i.php?path=$1;
}
# Catch .css.lens requests and serve cache when possible
location ~ "(lightbox-)?settings.css.lens$" {
default_type text/css;
try_files /storage/cache/site/${uri} /app/site/site.php?url=/$1settings.css.lens;
}
# Catch koken.js requests and serve cache when possible
location ~ koken.js$ {
default_type text/javascript;
try_files /storage/cache/site/${uri} /app/site/site.php?url=/koken.js;
}
# Standard site requests are cached with .html extensions
set $cache_ext 'html';
# PJAX requests contain the _pjax GET parameter and are cached with .phtml extensions
if ($arg__pjax) {
set $cache_ext 'phtml';
}
# Do not check for a cache for non-GET requests
if ($request_method != 'GET') {
set $cache_ext 'nocache';
}
# If share_to_tumblr cookie is preset, disable caching (long story)
if ($http_cookie ~* "share_to_tumblr" ) {
set $cache_ext 'nocache';
}
# Catch root requests
location ~ ^/?$ {
try_files /storage/cache/site/index/cache.$cache_ext /app/site/site.php?url=/;
}
# All other requests get passed back to Koken unless file already exists
location / {
try_files $uri $uri/ /storage/cache/site/${uri} /storage/cache/site/${uri}cache.$cache_ext /app/site/site.php?url=$uri&$args;
}
@bradleyboy
Copy link
Author

@Wiiilmaa Oops, thanks. Fixed now.

@bhays
Copy link

bhays commented Jan 20, 2014

Line 30 is missing an opening brace.

Thanks for putting this together.

@nvallet
Copy link

nvallet commented Feb 22, 2014

@bradleyboy Would be nice if you could fix line 30 as mentioned by bhays.

Thank you so much for impressive work with Koken and great support too.

@bradleyboy
Copy link
Author

@bhays @nvallet Sorry I missed this before now, just fixed line 30.

@csu
Copy link

csu commented Jan 26, 2015

Kind of struggling with this. New to using nginx. I put this into my sites-enabled file in the server block and now my root path ('/') just 404's. What did I do wrong?

@AstroProfundis
Copy link

I have added these lines to the file to prevent direct access to the original files:

# Original file directories
location ~ "^/storage/originals(/(([0-9]{2}/[0-9]{2}))/.*)$" {
    deny all;
}

It does not affect the "download" button on page when setting "Maximum image download" as "Extra large" (which i'm using), as download also goes to the cache folder. I didn't test on other options.

@meulie
Copy link

meulie commented Apr 9, 2016

Just like @csu I'm stuggling a bit as well to make this work with NGINX. Do we need more than just the above-mentioned config?

@kpmgeek
Copy link

kpmgeek commented May 9, 2016

Also struggling, the last section makes my NGINX config fail on Debian 8.2 with easyengine managing the nginx conf.

@rom3r4
Copy link

rom3r4 commented Oct 23, 2016

There a full Nginx configuration example here: https://github.com/koken/docker-koken-lemp/blob/master/conf/nginx-site.conf. Works fine for me on Debian Jessie

@trwnh
Copy link

trwnh commented Nov 28, 2017

How would I make this work for a subdirectory? I'm still getting /index.php? in my URLs.

@catgarret
Copy link

catgarret commented Feb 18, 2022

# Catch koken.js requests and serve cache when possible
location ~ koken.js$ {
    default_type text/javascript;
    try_files /storage/cache/site/${uri} /app/site/site.php?url=/koken.js;
}

is not working. T^T

A 404 error appears when accessing the address printed on the browser console
example.com/koken.js?0.22.24

But example.com/index.php? When approached, it comes out normally.
example.com/ndex.php?/koken.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment