Skip to content

Instantly share code, notes, and snippets.



Created Oct 16, 2011
What would you like to do?
Example microcache configuration
## Declare cache zone.
fastcgi_cache_path /opt/local/var/cache/nginx levels=1:2 keys_zone=microcache:5m max_size=1G;
map $http_cookie $no_cache {
default 0;
~_mcnc 1;
server {
listen 80;
access_log /opt/local/var/log/nginx/;
error_log /opt/local/var/log/nginx/ debug;
rewrite_log on;
root /data/;
index index.php index.html index.htm;
client_max_body_size 20m;
# Not found this on disk?
# Feed to CakePHP for further processing!
location / {
try_files $uri $uri/ /index.php?url=$uri;
## This kind of config is "insecure" a bad habit from Apache that gets propagated on Nginx.
## Try location = /index.php and see if it works.
location ~ \.php$ {
fastcgi_read_timeout 200;
fastcgi_index index.php;
fastcgi_intercept_errors on; # to support 404s for PHP files not found
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /opt/local/etc/nginx/fastcgi_params;
fastcgi_cache microcache;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
fastcgi_cache_key $scheme$host$request_method$request_uri;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
# Static files.
# Set expire headers, Turn off access log.
## Do you need a regex here? Can't you use an exact location? = /favicon.ico?
location ~* \favicon.ico$ {
access_log off;
expires 1d;
add_header Cache-Control public;
location ~ ^/(?:img|js|css|cjs|ccss)/ {
access_log off;
expires max;
add_header Cache-Control public;
# Deny access to .htaccess files,
# git & svn repositories, etc
location ~ /\.(?:ht|git|svn) {
deny all;

This comment has been minimized.

Copy link

@vitaoloureiro vitaoloureiro commented Jul 8, 2015

How to cache a mobile version of my site if i'm using the same url for both?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.