Clean-URL rewrite rule with nested "location" and alias directive

Ben Johnson ben at indietorrent.org
Tue Nov 19 15:36:53 UTC 2013


Hi!

I am attempting to serve a staging website from a directory that is
outside of the web-server's document root, while at the same time making
the site accessible at a URL that "appears" to be a subdirectory of the
top-level domain. (I have to do this so that the SSL certificate for the
TLD can be used for the staging site.)

This works as expected, with one major exception: "clean-URL" rewriting
does not work. In other words, the homepage (/stage/) loads correctly,
but all sub-pages return a 404. As soon as I attempt to add "rewrite"
directives for clean-URLs, the server either returns a 500 (redirect
cycle) or "Primary script unknown".

I have tried adding "rewrite_log on;" in the "server" block, just before
this configuration snippet (and reloading the nginx config), but the
directive seems to have no effect (no rewrite information is logged to
error.log). So, I am unable to determine what is happening here.

Here's the configuration snippet:

location ^~ /stage/ {
	alias /var/www/example.com/private/stage/web/;
	
	if ($scheme = http) {
		return 301 https://$server_name$request_uri;
	}
	
	index index.php index.html index.htm;
	
	location ~ ^/stage/(.+\.php)$ {
		alias /var/www/example.com/private/stage/web/$1;
		
		fastcgi_pass unix:/var/run/php5-fpm.sock;
		fastcgi_param HTTPS on;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;
		
		# Trying to implement clean-URLs with this line,
		# but it causes "primary script unknown" when using
		# the "break" keyword, and redirect cycle when using
		# the "last" keyword.
		rewrite '^(.*)$'  /stage/index.php?q=$1  break;
	}
}

Any assistance with making this to work is hugely appreciated. Thanks in
advance!

-Ben



More information about the nginx mailing list