Problem. I have one or more domains that serve some uri with a default upstream (a CMS) and few uris that require a different upstream (but still too many to list them comforably and too different to catch them with a regexp).
Now if all the configuration is the same for the two upstreams, beside the two upstreams in itself, I think I solved it in a Good Enough way, it is as follows:
map $host$request_uri $select_upstream_with_or_without_cache {
default http://upstream_def;
~^path1$ http://upstream_special;
~^path2$ http://upstream_special;
~^path3$ http://upstream_special;
~^path4$ http://upstream_special;
# remember! Here path is, beside the $host, wildly different even within the same domain
}
[...config...]
server {
server_name server-with-same-params.for-different-upstreams.example;
[...config...]
location / {
proxy_pass $select_upstream_with_or_without_cache;
proxy_read_timeout 90s;
proxy_connect_timeout 90s;
proxy_send_timeout 90s;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_hide_header Etag;
proxy_hide_header Accept-encoding;
proxy_hide_header Via;
}
}
What if, though, I need to pass different parameters to different upstreams? I didn't really find a clean solution. I found one using additional redirects (30X), but those aren't transparent to the user and the helper paths are shown in the browser, even if for a little time. Any ideas?
What I want to achieve is something like this:
map $host$request_uri $select_upstream_with_or_without_cache {
default go_to_location_upstream_def;
~^path1$ go_to_location_upstream_special;
~^path2$ go_to_location_upstream_special;
~^path3$ go_to_location_upstream_special;
~^path4$ go_to_location_upstream_special;
# remember! Here path is, beside the $host, wildly different even within the same domain
}
[...config...]
server {
server_name server-with-diff-params.for-different-upstreams.example;
[...config...]
# how to have two locations, with different configs, that are selected according to the
# uris (given that the uris are very different and not really caught by regexps) ?
# how to do something like the following ?
location @upstream_def {
proxy_pass http://upstream_def;
[configuration X]
}
location @upstream_special {
proxy_pass http://upstream_special;
[configuration Y]
}
}