drupal+nginx: this works => using try_files

Mark Alan varia at e-healthexpert.org
Tue Mar 17 12:36:58 MSK 2009


The following has been tested in Nginx  0.7.42 + Drupal 6.10 + Ubuntu 8.10.

M.


The tests included calls to:
# http://clinica.0/index.php		# ok
# http://clinica.0			# ok
# http://www.clinica.0			# ok
# http://clinica.0/user/password	# ok
# http://clinica.0/test			# Page not found
# http://nonexistant.clinica.0/		# Address Not Found
# http://clear.clinica.0/		# redirect to [clear.]clinica.0
#
# http://clinica.0/scripts/drupal.sh		# 403 Forbidden
# http://clinica.0/scripts/code-style.pl	# 403 Forbidden
# http://clinica.0/sites/default/settings.php	# 403 Forbidden
# http://clinica.0/sites/all/README.txt		# show README.txt
# http://clinica.0/sites/default/files/README.txt # show (*1)
# http://clinica.0/nice ports,/Trinity.txt.bak	# Page not found
#
# http://www.clinica.0/benchtest.php	# show a timed phpinfo (*2)
# http://www.clinica.0/somefile.php	# No input file specified.
# http://127.0.0.1/			# redirect to clinica.0
#
#(*1) this README.txt is a copy made from sites/all/README.txt
#(*2) see at the end of this email how benchtest.php was created

The file bellow does not fully take care of:
   ^/system/test/(.*)$ => /index.php?q=system/test/$1,
   ^/([^.?]*)\?(.*)$ => /index.php?q=$1&$2,
   ^/([^.?]*)$ => /index.php?q=$1
   ^/search/node/(.*)$" => /index.php?q=search/node/$1


### drupal-cgm file follows:
# --- cut here ---
# nginx - /etc/nginx/sites-available/drupal-cgm
#
server {
	listen 80 default;
	server_name _; # non defined server
	return 444; # silently drop request
}
server {
	listen clinica.0:80;
	server_name www.clinica.0;
	rewrite ^(.*) http://clinica.0$1 permanent;
}
server {
	listen clinica.0:80;
	server_name clinica.0;

	access_log /var/log/nginx/drupal-cgm.access.log;
	error_log /var/log/nginx/drupal-cgm.error.log;

	root /var/www/drupal-cgm;

	# restrict request methods to: GET|HEAD|POST
	if ( $request_method !~ ^(GET|HEAD|POST)$ ) {
		return 444;
		break;
	}

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

#	# rewrite domain name without the leading www.
#	if ($host ~* ^www\.(.*)) {
#		set $host_without_www $1;
#		# bellow $1 contains '/foo', not 'www.mydomain.com/foo'
#		rewrite ^(.*)$ http://$host_without_www$1 permanent;
#	}

	location / {
		index index.php index.html;
		try_files $uri $uri/ @drupal;
	}

	location @drupal {
		# clean url rewrite for Drupal
		rewrite ^/(.*)$ /index.php?q=$1 last;
		#rewrite ^/(\w*)/(.*)$ /$1/index.php?q=$2 last;
	}

	location ~ \.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_pass unix:/tmp/php-fastcgi.socket;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	}
}
# --- cut here ---


### benchtest.php file follows:
# --- cut here ---
sudo tee /var/www/drupal-cgm/benchtest.php <<-\EOA
<?php
// /var/www/drupal-cgm/benchtest.php
$start = microtime(true);
ob_start(); //Turn on output buffering
phpinfo();
$info = ob_get_clean();
echo "phpinfo() run time:  ";
printf("%0.1f ms\n", (microtime(true)-$start)*1000);
echo $info;
ob_end_flush();
?>
EOA
# --- cut here ---





More information about the nginx mailing list