server {
    listen  80;
    server_name  example1.com;
    access_log /var/log/nginx/example1.com.access.log;
    error_log /var/log/nginx/example1.com.error.log;
    root /var/www/example1.com;
    index index.php;

    location / {
        rewrite ^/(.*)/$ /$1 permanent; # remove trailing slashes
        try_files $uri @cache;
    }

    location @cache {
        if ( $request_method !~ GET ) { 
            return 405;
        }
        if ($http_cookie ~ "DRUPAL_UID") {
            return 405;
        }
        error_page 405 = @drupal;

        expires epoch;
        add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
        charset utf-8;

        try_files /cache/$host${uri}_$args.html @drupal;
    }

    location @drupal {
        rewrite ^/(.*)$ /index.php?q=$1 last;
    }

    location ~* (/\..*|settings\.php$|\.(htaccess|engine|inc|info|install|module|profile|pl|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(Entries.*|Repository|Root|Tag|Template))$ {
        deny all;
    }

    location ~ \.php$ {
        try_files $uri @drupal;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }

    location ~ \.css$ {
        if ( $request_method !~ GET ) {
            return 405;
        }
        if ($http_cookie ~ "DRUPAL_UID") {
            return 405;
        }
        error_page 405 = @uncached;

        access_log  off;
        expires  max; #if using aggregator

        try_files /cache/$host${uri}_.css $uri =404;
    }
    
    location ~ \.js$ {
        if ( $request_method !~ GET ) {
            return 405;
        }
        if ($http_cookie ~ "DRUPAL_UID") {
            return 405;
        }
        error_page 405 = @uncached;

        access_log  off;
        expires  max; # if using aggregator

        try_files /cache/$host${uri}_.js $uri =404;
    }

    location @uncached {
        access_log  off;
        expires  max;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico)$ {
        if ($http_referer !~ ^(http://example1.com) ) { # prevent image hijacking
            return 444;
        }

        access_log      off;
        expires         45d;

        try_files $uri =404;
    }
}
