Tech 18. Mar. 2016

TYPO3 with Nginx using RealURL and Static File Cache

While using the Docker TYPO3 Boilerplate and moving to a hoster who likes Nginx more than Apache, I gave Nginx a try for a TYPO3 installation.

So as expected nothing really failed. The only challenge was to configure Nginx for RealURL (quite easy) and Static File Cache (a little more advanced, because the given templates are not all complete).

So here my configuration of a production system with Nginx and TYPO3 6.x LTS:

location / {

	# serve exising files directly
	if (-f $request_filename) {
	  break;
	}

	if ($request_uri = "/typo3/") {
		rewrite .* /typo3/index.php last;
		return 200;
	}

	if ($request_uri = "/typo3/install/") {
		rewrite .* /typo3/install/index.php last;
		return 200;
		}
            
	# set to static delivery by default
	set $static 1;

	if ($request_uri ~ "\.(xml|css)$") {
	  set $static 0;
	}

	if ($http_pragma = 'no-cache') {
	  set $static 0;
	}

	if ($http_cache_control = 'no-cache') {
	  set $static 0;
	}

	if ($scheme = 'https') {
	  set $static 0;
	}

	if ($http_cookie = 'nc_staticfilecache|be_typo_user') {
	  set $static 0;
	}

	if ($request_method = 'POST') {
	  set $static 0;
	}

	if ($query_string) {
	  set $static 0;
	}

	if (!-f $document_root/typo3temp/tx_ncstaticfilecache/http/domainname${request_uri}index.html ) {
    	set $static 0;
	}

	# deliver static files
	if ($static = 1) {
	  rewrite .* /typo3temp/tx_ncstaticfilecache/http/domainname${request_uri}index.html break;
	  return 200;
	}

	if (!-f $request_filename) {
	  rewrite .* /index.php last;
	  return 200;
	}

}