ebbishop
ebbishop

Reputation: 1983

Nginx: redirect location based on multiple args

I have an nginx location redirect set up to redirect /my_route to a coming soon page, but allow /my_route?db=preview to pass through to the proxy server.

location /my_route {
  if ($arg_db != "preview") {
    rewrite ^ /coming-soon/ last;
  }
  <other location config for when db == preview>
}

I want to add an additional layer of complexity, to support multiple languages for the coming soon page.

Config that breaks nginx, but give you the idea:

location /my_route {
  if ($arg_db != "preview") {
    if ($arg_lang == "es") {
      rewrite ^ /coming-soon/index_es.html last;
    }
    rewrite ^ /coming-soon/ last;
  }
  <other location config for when db == preview>
}

I know that if is evil, so I'm happy to move away from using if, if that's what is needed, but I don't know which direction to look. I just know nginx doesn't support && statements, or nested if statements.

Upvotes: 0

Views: 445

Answers (1)

Richard Smith
Richard Smith

Reputation: 49792

You could use a map to eliminate the inner if block.

For example:

map $arg_lang $mylang {
    default   /;
    es        /index_es.html;
}
server {
    ...
    rewrite ^ /coming_soon$mylang last;
    ...
}

See this document for details.

Upvotes: 1

Related Questions