OP bir örneği tercih etti. Ayrıca, @minaev'in yazdığı şey, hikayenin sadece bir parçasıydı! İşte başlıyoruz.
Örnek 1: Hayır (ara veya son) bayrak
server {
server_name example.com;
root 'path/to/somewhere';
location / {
echo 'finally matched location /';
}
location /notes {
echo 'finally matched location /notes';
}
location /documents {
echo 'finally matched location /documents';
}
rewrite ^/([^/]+.txt)$ /notes/$1;
rewrite ^/notes/([^/]+.txt)$ /documents/$1;
}
Sonuç:
# curl example.com/test.txt
finally matched location /documents
Açıklama:
İçin rewrite, bayraklar isteğe bağlıdır!
Örnek 2: Dış konum bloğu (ara veya son)
server {
server_name example.com;
root 'path/to/somewhere';
location / {
echo 'finally matched location /';
}
location /notes {
echo 'finally matched location /notes';
}
location /documents {
echo 'finally matched location /documents';
}
rewrite ^/([^/]+.txt)$ /notes/$1 break; # or last
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
}
Sonuç:
# curl example.com/test.txt
finally matched location /notes
Açıklama:
Konum bloğunun dışında, her ikisi de breakve lastaynı şekilde davranın ...
- artık yeniden yazma koşullarının ayrıştırılması yok
- Nginx iç motoru bir sonraki aşamaya gider (
locationeşleşme aranıyor)
Örnek 3: İç konum bloğu - "break"
server {
server_name example.com;
root 'path/to/somewhere';
location / {
echo 'finally matched location /';
rewrite ^/([^/]+.txt)$ /notes/$1 break;
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
}
location /notes {
echo 'finally matched location /notes';
}
location /documents {
echo 'finally matched location /documents';
}
}
Sonuç:
# curl example.com/test.txt
finally matched location /
Açıklama:
Bir konum bloğunun içinde breakbayrak aşağıdakileri yapar ...
- artık yeniden yazma koşullarının ayrıştırılması yok
- Nginx dahili motor mevcut
locationbloğu ayrıştırmaya devam ediyor
Örnek 4: İç konum bloğu - "son"
server {
server_name example.com;
root 'path/to/somewhere';
location / {
echo 'finally matched location /';
rewrite ^/([^/]+.txt)$ /notes/$1 last;
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed
}
location /notes {
echo 'finally matched location /notes';
rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed, either!
}
location /documents {
echo 'finally matched location /documents';
}
}
Sonuç:
# curl example.com/test.txt
finally matched location /notes
Açıklama:
Bir konum bloğunun içinde lastbayrak aşağıdakileri yapar ...
- artık yeniden yazma koşullarının ayrıştırılması yok
- Nginx dahili motoru , sonucun sonucuna göre başka bir yer eşleşmesi aramaya başlar
rewrite .
- bir dahaki konum maçında bile tekrar yazma koşullarının ayrıştırılması yok!
Özet:
rewriteBayrakla breakveya lasteşleşmeyle ilgili bir durum olduğunda , Nginx artık ayrıştırmayı durdurur rewrites!
- Bir konum bloğu dışında
breakveya last, Nginx aynı işi yapar (koşullara yeniden artık işlemeyi bırakır).
- Bir konum bloğunun içinde
break, Nginx yalnızca artık yeniden yazma koşullarını işlemeyi durdurur
- Bir konum bloğunun içinde
last, Nginx artık tekrar yazma koşullarını işlemeyi durdurur ve ardından yeni bir blok eşleşmesi aramaya başlarlocation ! Nginx ayrıca rewritesyeni locationbloktaki herkesi görmezden geliyor !
Son Not:
Bazı daha ileri vakaları dahil etmeyi özledim (aslında yeniden yazma ile ilgili sık görülen bir problem 500 internal error). Ancak bu, bu sorunun kapsamı dışında kalacaktı. Muhtemelen, örnek 1 de kapsam dışı!