Score:0

Setup A Default Redirect In Nginx

cn flag

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/;
  }
}
Score:0
nr flag

The return 301 rule, when outside of any location blocks, will apply to the whole server block and take precedence over the location blocks. You could instead define a default/fallback location block, as in your try #2, but without the equals sign (=). The equals sign specifies an exact match, and you instead want a prefix match so that it will match all requests.

For example:

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

  location / {
     return 301 http://$host/home/;
  }
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.