Настройте ловушку внутри местоположения с помощью отката к index.php

3141
Rudie

Я борюсь с конфигом nginx. У меня есть serverблок, в который я хочу отправить все запросы index.php?tags=$uri, если они не $uriсуществуют (например, index.php?a=bили /?a=b).

Я бы ожидал:

try_files $uri index.php?tags=$uri; 

но нет, это было бы слишком просто. Это не работает /?a=b, который, по-видимому, не найден, поэтому он направляетindex.php?tags=/

Может быть, если я явно включу index, что является разумным:

index index.php; 

Нету. Нет кости. Точно такой же результат по всем направлениям.

Также ничего $args, $request_uriили комбинации. Тоже не это:

try_files $request_uri/index.php $request_uri index.php?tags=$request_uri; // now I'm just guessing 

Apache почему-то знал, что я имел в виду всегда. Почему не nginx? Я хотел бы эти перенаправления (без redirectили if):

/ => /index.php /index.php => /index.php /index.php?a=b => /index.php?a=b /?a=b => /index.php?a=b /foo => /index.php?tags=foo (or /index.php?tags=/foo) /foo/bar => /index.php?tags=foo/bar (or /index.php?tags=/foo/bar) /foo?bar=yes => /index.php?tags=/foo%3Fbar%3Dyes 

Я бы хотел, чтобы строка запроса кодировалась при перенаправлении, но не путь, но на самом деле это не так важно.

(Я также не понимаю практической разницы между $ uri и $ request_uri. Кажется, они делают то же самое половину времени. Но это на другой день.)

Большое спасибо.

2

1 ответ на вопрос

2
Bernard Rosset

I am achieving the wished result with the following configuration snippet:

location = / { index index.php; } location / { try_files $uri /index.php?tags=$request_uri; } 

try_files tries... files. When you seek / with it, you search for a file with the same name, it is not interpreted as 'find the index file'. index does that job. Hence you need to separate this special case from the default, fall-back location.

The best part is your last wish: the arguments won't even be encoded since they do not need to (only the first question mark of a URI is relevant as everything following is an argument anyway).

Mind the use of $request_uri (which contains the requested URI, with arguments, but does not normalize/cleanse it) instead of the normalized $uri (which cleans up the URI and remove arguments). Thus you may end up with:

///foo?bar=yes => index.php?tags=///foo?bar=yes 

If you mind about it, you may use $uri in combination with $args:

location = / { index index.php; } location / { try_files $uri /index.php?tags=$uri?$args; } 

producing:

///foo?bar=yes => index.php?tags=/foo?bar=yes 
`Location = /` сделал свое дело. Почему вы не можете объединить это внутри блока `location /`? Означает ли `=`, что оно соответствует только точному URI? Спасибо. Они говорят, что nginx легко. Rudie 9 лет назад 0
Все подробно объясняется в [`location`] (http://nginx.org/en/docs/http/ngx_http_core_module.html#location) документации, которую я предлагаю вам прочитать. Посмотрите на разные модификаторы. Bernard Rosset 9 лет назад 0

Похожие вопросы