I
I
igordata2014-08-05 23:32:11
Nginx
igordata, 2014-08-05 23:32:11

nginx config: Mysterious redirect loop when using a variable in the server_name directive. Where are the loot?

Hello. Faced with mysticism.
There is a config (one file):

server {
  server_name  www.test.ru;
  return        301 http://test.ru$request_uri;
}

server {
  set $somevar test.ru;
  listen   80;
  server_name  $somevar;
  root   /var/www/$somevar;
..и др.

When using it, there is an eternal redirect from test.ru even without www.
If you remove a piece
server {
  server_name  www.test.ru;
  return        301 http://test.ru$request_uri;
}

Or remove the variable from server_name
server_name test.ru;
Then everything will work like clockwork.
I don't understand the problem mechanism. Where are the loot?
Thanks for reading. I hope for answers. Any ideas are welcome.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2014-08-06
@Lynn

The `server_name` directive cannot contain variables.
As a result, you have two servers, one named `www.test.ru`, the second named `$somevar`.
The request to the `test.ru` server does not find a server with the same name, gets to the default server, in this case the first one, and an endless redirect is obtained.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question