The following script is a working Nginx configuration for Drupal with Boost and clean URL for 6, 7 and 8 support:
http {
...
## Parse $uri and $args to correctly match the static Boost generated files
## Remove the trailing slash and "/index.php"
map $uri $boost_uri {
default $uri;
~^/(.*)/$ $1;
~^/(.*) $1;
~(.*)/$ $1;
/index.php '';
}
## Remove the trailing slash in "q" query variable parameter
map $arg_q $boost_argq {
default $arg_q;
~^/(.*) $1;
}
server {
server_name test7.webfoobar.com;
root /var/www
index index.php;
location @cache {
## Boost configuration
gzip_static on;
add_header Expires "Tue, 25 Dec 1977 03:45:00 GMT";
add_header Cache-Control "must-revalidate, post-check=0, pre-check=0";
add_header Drupal-Cache "Boost";
charset utf-8;
try_files /cache/normal/$host/$boost_uri${boost_argq}_.html /cache/normal/$host/$boost_uri${boost_argq}_.xml /cache/normal/$host/$boost_uri${boost_argq}_.json /cache/perm/$host/$boost_uri${boost_argq}_.js /cache/perm/$host/$boost_uri${boost_argq}_.css;
}
location @indexphp {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
location / {
## Let Drupal handle 404
error_page 404 /index.php;
## Drupal generated static files
include apps/drupal/static_files_handler.conf;
## Handler for POST request
error_page 419 = @indexphp;
## No caching for POST requests.
if ($request_method = POST) {
return 419;
}
## Handler to redirect to boost cache named location.
error_page 420 = @cache;
## Check if static boost cache file already generated.
if (-f $document_root/cache/normal/$host/$boost_uri${boost_argq}_.html) {
## Redirect to boost cache named location.
return 420;
}
if (-f $document_root/cache/normal/$host/$boost_uri${boost_argq}_.xml) {
## Redirect to boost cache named location.
return 420;
}
if (-f $document_root/cache/normal/$host/$boost_uri${boost_argq}_.json) {
## Redirect to boost cache named location.
return 420;
}
if (-f $document_root/cache/perm/$host/$boost_uri${boost_argq}_.js) {
## Redirect to boost cache named location.
return 420;
}
if (-f $document_root/cache/perm/$host/$boost_uri${boost_argq}_.css) {
## Redirect to boost cache named location.
return 420;
}
## No Boost cache file generated yet.
try_files $uri $uri/ @indexphp;
}
location ~* ^/(?:index|boost_stats)\.php {
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME /index.php;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
fastcgi_param HTTP_HTTPS $https;
fastcgi_param HTTPS $https;
fastcgi_pass www;
fastcgi_buffers 256 4k;
fastcgi_intercept_errors on;
fastcgi_read_timeout 14400;
fastcgi_index index.php;
}
## Any other attempt to access PHP files returns a 404.
location ~* ^.+\.php$ {
return 404;
}
}
}
You may wonder why there are "if" conditions checking for generation of Boost cache files and why nor replaced them with the codes inside the named location "@cache". Notice the line add_header Drupal-Cache "Boost";
, this will be a useful indicator if Boost is working and the cache file is already generated successfully. If we directly use the codes inside the named location "@cache" in location /
, all the requests will have headers containing Drupal-Cache: Boost
(the indicator will be useless). In the screen shot below, the Boost cache file is not yet generated:
After a browser refresh, we can see now that the Boost cache file is already generated. We can say that the script calls the "@cache" as the Boost cache file already exist:
The script of apps/drupal/static_files_handler.conf
can be found here.