Rewrite and FastCGI.

Francis Daly francis at
Wed Oct 10 12:36:16 UTC 2012

On Tue, Oct 09, 2012 at 04:50:32PM +0200, Thomas Martin wrote:

Hi there,

great that you found a configuration that works.

> FYI I read the documentation before to send my first email but I
> misunderstood the part about "^~" and also I didn’t clearly realized
> that "One request is handled in one location".

Having re-read the docs, I can see that the interpretation could be
made clearer. Can you suggest any wording that might have helped you
understand then, what you do now? Maybe it can become easier for the
next person ;-)

Would adding a link to
have helped, do you think? It is an example rather than complete
documentation, and it leaves out the "^~" thing, so maybe it wouldn't
have been directly useful.

> So with your clarification I was able to make something working:

> 	location /dir1 {
> 		rewrite ^(.*) https://$host$1 permanent;
> 	}

> 	location /dir2 {
> 		# php5-fpm
> 		location ~ \.(php|phtml)$ {
> 			fastcgi_param	PHP_VALUE "include_path=/www/dir2:/www/dir2/common/include:.";
> 			fastcgi_param	ENVIRONMENT_NAME devs;
> 			fastcgi_param	DB_CONF_DIRECTORY /etc/itf/db/devs/;
> 			include /etc/nginx/sites-conf.d/php-fpm;
> 		}
> 	}

>     location / {
> 		# php5-fpm
> 		location ~ \.(php|phtml)$ {
> 			fastcgi_param	ENVIRONMENT_NAME devs;
> 			fastcgi_param	DB_CONF_DIRECTORY /etc/db/devs/;
> 			include /etc/nginx/sites-conf.d/php-fpm;
> 		}
>     }
> It seems to work as expected; I guess I could use "^~" too but I
> didn't tried yet.

That looks reasonable. "^~" only matters if you have (top level) regex
matches -- which here, you don't.

(Avoiding top level regex matches makes it very easy to know which
location{} will match any particular request. That's usually considered
a Good Thing.)

> At first I wanted to avoid repetition of the php-fpm's part but I
> didn't realized that it wasn't doable.

In this case, you want different fastcgi_param lines for different php
scripts -- so repeating the common config (by using "include") and adding
the specific parts, like you have done, is probably the best way.

> Thanks for your help and again this is really appreciated!

You're welcome.

All the best,

Francis Daly        francis at

More information about the nginx mailing list