D
D
Daniil Muidinov2015-12-02 14:43:34
Nginx
Daniil Muidinov, 2015-12-02 14:43:34

What is the correct way to use return instead of rewrite in nginx config?

Good day.
After watching the report by Igor Sysoev on HL++ https://vimeo.com/117154512 , I concluded that it is better to avoid using regular expressions and rewrites in the Nginx configuration, but I don’t know how to avoid this in the following situation, you need to rewrite the following

location /tst_
  rewrite ^/tst_([0-9]+)_(.+) /?mode=feed&feed=$1&token=$2 last;
  rewrite ^/tst_([0-9]+) /?mode=feed&feed=$1 last
}

I can avoid it like this, but then I have to resort to regular expressions
location ~/tst_([0-9]+) {
  return 302 http://example.ru/?mode=feed&feed=$1;
  location ~/tst_([0-9]+)_(.+) {
    return 302 http://example.ru/?mode=feed&feed=$1&token=$2;
  }
}

What is the priority to use? Or is it possible to somehow avoid both rewrites and regular expressions (or escape the regular expression in this case)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2015-12-02
@Lynn

You made some wrong thought.
Correct: if possible, avoid location with regular expressions.
Your first option will run faster. In addition, it is not equivalent to the second, because it does not cause a redirect.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question