M
M
medianoche2016-11-07 12:49:58
Nginx
medianoche, 2016-11-07 12:49:58

Why is replacement not working in nginx rewrite?

location / {
  if ($args ~ "_escaped_fragment_=(.*)") {
    rewrite ^ /snapshot/$1/index.html;
    break;
  }

  try_files $uri $uri/ /index.html =404; # html5Mode
}

Gives an error message:
open() "/home/www/site.ru/public/snapshot//index.html" failed (2: No such file or directory),
request: "GET /?_escaped_fragment_=tools HTTP/1.1", host: "site.ru"

It looks like it's trying to open public//snapshot // index.html and inserting $1 doesn't work. Apparently this is due to the fact that the replacement occurs in the location section. How to fix this error?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2016-11-07
@medianoche

You expect $1 to be taken from the regex in the if, but it's actually taken from the regex in rewrite, i.e. empty
Use $arg_* variables. For example
rewrite ^ /snapshot/$arg__escaped_fragment_/index.html;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question