E
E
Eugene2021-03-16 20:30:40
Nginx
Eugene, 2021-03-16 20:30:40

How to organize nginx condition check?

There is a task to check the parameter in the address bar and, depending on the value, either process it in the current server section or redirect to another server.
The check happens with the help of lua, but it is not possible to send a request further.
Tried the following options:

rewrite_by_lua_block {
  ngx.exec("@asd")
}
location @asd {
  echo "asd";
}

the output is rewrite or internal redirection cycle while redirect to named location "@asd"
set $new "0";
rewrite_by_lua_block {
  ngx.var.new="1"
}

if ($new = "1") {
 	rewrite ^(.*)$ /to_new_server/$1;
}

location / {
  echo "no";
}
location /to_new_server/ {
  echo "asd";
}

the "/" section is executed. while $new is actually equal to 1.
ngx.req.set_uri("/to_new_server", true)
rewrite or internal redirection cycle while processing "/to_new_server"
local uri = ngx.re.sub(ngx.var.uri, "^(.*)$", "/to_new_server/$1")
ngx.req.set_uri(uri)

sends to the "/" section.

Please help me to solve the problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question