Problem with / location stanza

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/ - 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.

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/:

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

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
[email protected]

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.

http://wiki.nginx.org/IfIsEvil

Maxim D.

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.)(?.+)$;

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. [email protected]

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. [email protected]


nginx mailing list
[email protected]
http://nginx.org/mailman/listinfo/nginx


Michael Barrett
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs