nginx different routes for / and /*
I'd like to route /
to a.sock
and /(.*)
to b.sock
.
I tried
location / {
proxy_pass http://unix:/tmp/a.sock;
proxy_read_timeout 30;
proxy_connect_timeout 30;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ /(.*) {
proxy_pass http://unix:/tmp/b.sock;
proxy_read_timeout 30;
proxy_connect_timeout 30;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
with different ordering too.
Can this be achived with nginx, if so, how?
My take is, according to the documentation anything ending with a /
assumes a wildcard after the /
and the plain route without the regex takes precedence.
Essentially the backend on b.sock
has a route definition of /:term
or /{term}
depending on which routing lib you use, but has no handler for /
.
You're probably thinking "so add a handler for /
and call it a day". I might do that, but it's a workaround. I would like a.sock
to handle /
.
So again, can this be done with nginx and how?
Use exact matching for homepage, e.g. location = / { … }
. The location / { … }
will apply for the rest of the pages.
The exact matching (with equals sign) has priority over prefix location (no equals sign).