OP ต้องการตัวอย่าง สิ่งที่ @minaev เขียนนั้นเป็นเพียงส่วนหนึ่งของเรื่องราว! ดังนั้นเราไปที่นี่ ...
ตัวอย่างที่ 1: ไม่มีแฟล็ก (ตัวแบ่งหรือล่าสุด)
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;
}
ผลลัพธ์:
# curl example.com/test.txt
finally matched location /documents
คำอธิบาย:
สำหรับrewriteธงเป็นตัวเลือก!
ตัวอย่างที่ 2: บล็อกตำแหน่งนอก (ตัวแบ่งหรือล่าสุด)
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
}
ผลลัพธ์:
# curl example.com/test.txt
finally matched location /notes
คำอธิบาย:
นอกบล็อกตำแหน่งที่ตั้งทั้งสองbreakและlastทำงานในลักษณะที่แน่นอน ...
- ไม่มีการแยกวิเคราะห์เงื่อนไขอีกต่อไป
- เอ็นจิ้นภายใน Nginx ไปที่ขั้นตอนต่อไป (ค้นหาการ
locationจับคู่)
ตัวอย่างที่ 3: บล็อกตำแหน่งภายใน - "ตัวแบ่ง"
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';
}
}
ผลลัพธ์:
# curl example.com/test.txt
finally matched location /
คำอธิบาย:
ภายในบล็อกตำแหน่งที่ตั้งจะตั้งbreakค่าสถานะต่อไปนี้ ...
- ไม่มีการแยกวิเคราะห์เงื่อนไขอีกต่อไป
- เอ็นจิ้นภายใน Nginx ยังคงแยกวิเคราะห์
locationบล็อกปัจจุบัน
ตัวอย่างที่ 4: บล็อกตำแหน่งภายใน - "สุดท้าย"
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';
}
}
ผลลัพธ์:
# curl example.com/test.txt
finally matched location /notes
คำอธิบาย:
ภายในบล็อกตำแหน่งที่ตั้งจะตั้งlastค่าสถานะต่อไปนี้ ...
- ไม่มีการแยกวิเคราะห์เงื่อนไขอีกต่อไป
- เอ็นจิ้นภายใน Nginx เริ่มมองหาการจับคู่ตำแหน่งอื่นตามผลลัพธ์ของ
rewriteผลลัพธ์
- ไม่มีการแยกวิเคราะห์เงื่อนไขการเขียนอีกต่อไปแม้ในการจับคู่ตำแหน่งต่อไป!
สรุป:
- เมื่อ
rewriteเงื่อนไขกับการตั้งค่าสถานะbreakหรือการlastแข่งขัน Nginx หยุดการแยกวิเคราะห์อีกต่อไปrewrites!
- นอกบล็อกที่ตั้งด้วย
breakหรือlastNginx ทำงานเดียวกัน (หยุดการประมวลผลอีกครั้งเขียนเงื่อนไข)
- ภายในบล็อกตำแหน่งด้วย
breakNginx จะหยุดการประมวลผลการเขียนเงื่อนไขอีกต่อไป
- ภายในบล็อกตำแหน่งด้วย
lastNginx หยุดการประมวลผลอีกครั้งเขียนเงื่อนไขแล้วเริ่มมองหาการจับคู่locationบล็อกใหม่! Nginx ยังเพิกเฉยrewritesต่อlocationบล็อกใหม่!
หมายเหตุสุดท้าย:
ฉันพลาดที่จะรวมกรณีขอบเพิ่มเติม (จริง ๆ แล้วปัญหาทั่วไปกับ rewrites เช่น500 internal error) แต่นั่นไม่ได้อยู่ในขอบเขตของคำถามนี้ อาจเป็นไปได้ว่าตัวอย่างที่ 1 อยู่นอกขอบเขตเช่นกัน!