# Problem with / location stanza

**URL:** <https://www.ruby-forum.com/t/problem-with-location-stanza/205712>\
**Category:** NGINX\
**Created:** [April 14, 2011, 12:06am UTC](https://www.ruby-forum.com/t/problem-with-location-stanza/205712 "2011-04-14T00:06:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [April 14, 2011, 12:06am UTC](https://www.ruby-forum.com/t/problem-with-location-stanza/205712/1 "2011-04-14T00:06:36Z")

</div>

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. So basically:

[http://www.example.com/](http://www.example.com/) - Goes to the uwsgi process  
[http://foo.example.com/](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/](http://foo.example.com/bar/) - Is dealt with in other location stanzas.

Anyway, here’s the location stanza I’m trying to use:

```
location = / {
    if ($host ~ ^([^.]+)\.) {
        set $hn $1;
    }

    if ($hn !~ ^www\.) {
        set $vanity $hn;
    }

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

```

For some reason, whenever I have this in my config I get the following  
error when I try to hit [http://foo.example.com/:](http://foo.example.com/:)

Apr 13 14:53:01 stage-djcore-www1 nginx: 2011/04/13 14:53:01 [error]  
23919#0: \*3227 directory index of  
“/usr/local/www/eventbrite/releases/current/python/” is forbidden,  
client: 127.0.0.1, server: , request: “GET / HTTP/1.1”, host:  
“[foo.example.com](http://foo.example.com)”

If I don’t have that in my config, then this stanza is hit, and it sends  
everything back to my uwsgi server:

```
location / {
    uwsgi_pass ebdjango_uwsgi;
}

```

Anyone have any idea why this might be? Thanks in advance for your  
help, I’m banging my head against the table.

–  
Michael Barrett  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Michael\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Michael\_Barrett](https://www.ruby-forum.com/u/Michael_Barrett)\
**Post date:** [April 14, 2011, 12:38am UTC](https://www.ruby-forum.com/t/problem-with-location-stanza/205712/2 "2011-04-14T00:38:05Z")

</div>

Hello!

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

> stanzas.  
> }  
> “/usr/local/www/eventbrite/releases/current/python/” is  
> Anyone have any idea why this might be? Thanks in advance for  
> your help, I’m banging my head against the table.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.

---

<div class="post-metadata">

**Author:** ![Michael\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Michael\_Barrett](https://www.ruby-forum.com/u/Michael_Barrett)\
**Post date:** [April 14, 2011, 2:16am UTC](https://www.ruby-forum.com/t/problem-with-location-stanza/205712/3 "2011-04-14T02:16:27Z")

</div>

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.

[Server names](http://nginx.org/en/docs/http/server_names.html) has details.

> [http://www.example.com/](http://www.example.com/) - Goes to the uwsgi process  
> [http://foo.example.com/](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/](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.)(?.+)$;

and

server\_name ~^(?[^.]+).(?.+)$;

(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 D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Michael\_Barrett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ec8ea/32.png) [@Michael\_Barrett](https://www.ruby-forum.com/u/Michael_Barrett)\
**Post date:** [April 15, 2011, 2:58am UTC](https://www.ruby-forum.com/t/problem-with-location-stanza/205712/4 "2011-04-15T02:58:22Z")

</div>

Thanks Maxim & Francis - I think I’m just doing it the wrong way to  
begin with. I’ve decided instead to try to use the uwsgi caching  
configs.

So I’m playing with that now, and I can’t seem to get my app to cache a  
certain location. Here’s the basic config I’m using:

(In my http{}):

uwsgi\_cache\_path /data/nginx/cache levels=1:2 keys\_zone=my-cache:10m;  
uwsgi\_temp\_path /data/nginx/tmp;

(then in my server{}):

```
location /event/1340113315 {
    uwsgi_pass ebdjango_uwsgi;
    uwsgi_cache my-cache;
    uwsgi_cache_valid any 10m;
    uwsgi_cache_key djcore$request_uri;
    access_log syslog test-cache;
    access_log /var/log/nginx/dj_access.log evb;
}

```

When I run with this, and hit that URL, I’m seeing the test-cache log  
message showing up in my logfiles, so it’s definitely hitting that  
location block. That said, I don’t see any files being created in  
/data/nginx/cache. Also, when doing an strace of the nginx process, I  
see it passing the query back to the uwsgi process, so it doesn’t look  
like the cache is being used at all.

Any hints on what I’m doing wrong here? I get the feeling it’s  
something dead simple, and I’m just glazing over it.

Thanks!

On Apr 13, 2011, at 5:15 PM, Francis D. wrote:

> server\_name values.  
> If you can enumerate your server names, that would be ideal. If not,
> 
> Good luck with it,
> 
> ## f
> 
> Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
Michael Barrett  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
