I need a way to redirect clients when no existing path is defined. When I put a return 301 config in, nginx seems to ignore any location configs. It redirects everything.
The hostname in the redirection needs to be dynamic (come from the client). These servers are actually containers and are deployed to Dev/Prod environments. So the client url changes from something like dev.example.com to example.com. I'd rather not do config swapping based on environments.
I'm using v1.18 on RHEL. The servers proxied are Angular apps managed by their respective developers.
server {
listen 80;
server_name _;
index index.html;
location = /service/a {
proxy_pass http://svc-a.local/service/a/;
}
location /service/a/ {
proxy_pass http://svc-a.local/service/a/;
}
location = /service/b {
proxy_pass http://svc-b.local/service/b/;
}
location /service/b/ {
proxy_pass http://svc-b.local/service/b/;
}
location = /service/x {
proxy_pass http://svc-x.local/service/x/;
}
location /service/x/ {
proxy_pass http://svc-x.local/service/x/;
}
location = /home {
proxy_pass http://home.local/home/;
}
location /home/ {
proxy_pass http://home.local/home/;
}
# kubernetes probes this, but fails getting 301
location /nginx_status {
stub_status on;
acccess_log off;
}
# IF NO MATCH FROM ABOVE THEN GO TO /HOME
# try #1
return 301 http://$host/home/;
# try #2
location = / {
return 301 http://$host/home/;
}
# try #3
return 301 /home/;
# try #4
location = / {
proxy_pass http://home.local/home/;
}
}