Tuesday, 1 October 2013

Why does nginx sometimes silently redirect?

Why does nginx sometimes silently redirect?

Nginx.conf, in brief:
...
server {
listen 80;
server_name ec2-54-234-175-21.compute-1.amazonaws.com;
location / {
root /var/www/static/;
default_type text/html;
try_files $uri $uri/index.html /index.html;
}
location /api/2.0 {
...
proxy_pass http://localhost:8080/;
}
location /api/ {
...
proxy_pass http://localhost:8000/;
}
}
I have two APIs. Essentially, I am trying to make them at reside at the
URIs of /api/1.0 and /api/2.0 respectively. Because of the internal
routing of the 1.0 app, it appends the 1.0 part, so to achieve the effect
I want, the nginx location for it is simply at /api/.
Without exactly understanding the location precedence rules that Nginx
uses, I was hoping /api/ coming after /api/2.0 in my configuration would
cause all things that began with /api/ but are not /api/2.0 to fall into
this second location block. This seems to work, however it is very
inconsistent.
The biggest problem--and I can't seem to find rhyme or reason to it--is
that sometimes /api/1.0 redirects to just /1.0, resulting in it being
handled by my try_files clause. If I put a full URI into the address bar,
'localhost/api/1.0/countyinmate?format=json', sometimes it redirects to
'localhost/1.0/countyinmate?format=json'. But then if I edit the URI by
putting 'api/' back in, this usually works. Anytime this problem occurs,
nginx is oblivious to it, and records in my access log that /1.0 was
accessed.
Any help is appreciated, sorry if I'm missing something obvious.

No comments:

Post a Comment