problem with / location stanza

Francis Daly francis at daoine.org
Thu Apr 14 04:15:45 MSD 2011


On Wed, Apr 13, 2011 at 03:06:04PM -0700, Michael Barrett wrote:

Hi there,

> I'm trying to make it so that if I get a request to / on my host to any hostname that doesn't start with 'www' it will check for a static page on the server, and if that doesn't exist it goes back to my uwsgi process for dynamic content. 

As you've seen, "if" inside "location" does probably not do what you
want it to.

The usual nginx way is to have multiple server{} blocks with different
server_name values.

http://nginx.org/en/docs/http/server_names.html has details.

> http://www.example.com/ - Goes to the uwsgi process
> http://foo.example.com/ - Checks for a static file (/landing-cache/vanity:foo.html), if it can't find it it goes to the uwsgi process
> http://foo.example.com/bar/ - Is dealt with in other location stanzas.

In your "not-www" server block, include the "location = /" block with
the try_files. In your "www" server block, don't.

If you can enumerate your server names, that would be ideal. If not,
perhaps you can use one "server_name www.*" and have the other be the
default. And failing that, then perhaps something like

  server_name ~^(www\.)(?<domain>.+)$;

and

  server_name ~^(?<vanity>[^.]+)\.(?<domain>.+)$;

(which would give you $domain, as well as $vanity) could work.

>         try_files /landing-cache/vanity:$vanity.html @ebdjango_uwsgi;


Or possibly, avoid all that and just set $vanity as the first bit of
$host or $http_host, and make sure that /landing-cache/vanity:www.html
does not exist.

It probably depends on what other differences you want between the
various server_name configurations.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list