OP lebih suka contoh. Juga, apa yang ditulis @minaev, hanyalah sebagian dari cerita! Jadi, ini dia ...
Contoh 1: Tidak ada tanda (putus atau terakhir)
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;
}
Hasil:
# curl example.com/test.txt
finally matched location /documents
Penjelasan:
Sebab rewrite, bendera itu opsional!
Contoh 2: Blok lokasi di luar (istirahat atau terakhir)
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
}
Hasil:
# curl example.com/test.txt
finally matched location /notes
Penjelasan:
Di luar blok lokasi, keduanya breakdan lastberperilaku dengan cara yang tepat ...
- tidak ada lagi penguraian kondisi penulisan ulang
- Mesin internal Nginx pergi ke fase berikutnya (mencari
locationkecocokan)
Contoh 3: Blok lokasi di dalam - "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';
}
}
Hasil:
# curl example.com/test.txt
finally matched location /
Penjelasan:
Di dalam blok lokasi, breakbendera akan melakukan hal berikut ...
- tidak ada lagi penguraian kondisi penulisan ulang
- Mesin internal Nginx terus mengurai
locationblok saat ini
Contoh 4: Blok lokasi di dalam - "terakhir"
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';
}
}
Hasil:
# curl example.com/test.txt
finally matched location /notes
Penjelasan:
Di dalam blok lokasi, lastbendera akan melakukan hal berikut ...
- tidak ada lagi penguraian kondisi penulisan ulang
- Mesin internal Nginx mulai mencari kecocokan lokasi lain berdasarkan
rewritehasil.
- tidak ada lagi penguraian kondisi penulisan ulang, bahkan pada pencocokan lokasi berikutnya!
Ringkasan:
- Ketika
rewritekondisi dengan bendera breakatau lastpertandingan, Nginx berhenti mengurai lagi rewrites!
- Di luar blok lokasi, dengan
breakatau last, Nginx melakukan pekerjaan yang sama (berhenti memproses lagi kondisi penulisan ulang).
- Di dalam blok lokasi, dengan
break, Nginx hanya berhenti memproses lagi kondisi menulis ulang
- Di dalam blok lokasi, dengan
last, Nginx berhenti memproses lagi kondisi penulisan ulang dan kemudian mulai mencari pencocokan locationblok yang baru! Nginx juga mengabaikan semua yang ada rewritesdi locationblok baru !
Catatan Akhir:
Saya rindu untuk memasukkan beberapa kasus tepi (sebenarnya masalah umum dengan penulisan ulang, seperti 500 internal error). Tapi, itu akan keluar dari ruang lingkup pertanyaan ini. Mungkin, contoh 1 juga di luar jangkauan!