search

Home  >  Q&A  >  body text

The difference between nginx rewrite without break and with break

A problem found when configuring nginx rewrite

if (!-e $document_root$fastcgi_script_name) {
    rewrite ^.*$ /404.html break;
}

This is normal, but after removing break,

if (!-e $document_root$fastcgi_script_name) {
    rewrite ^.*$ /404.html;
}

An error was reported at this time

rewrite or internal redirection cycle while processing "/404.html

What is the difference between adding and not adding break? When not adding break, what happens after rewrite? Ask God to clarify my doubts

--------renew---------

404.html file exists

When I change break to last, the above error will still be reported, so I think that without adding break or changing break to last, when the uri matches the above rewrite, it will match the location block again. , but the values ​​of variables ($document_root, $fastcgi_script_name...) and so on have not changed, causing the if block to be entered again, and the loop continues until more than 10 times, and then a 500 error is reported

--------Update again---------

After more testing, it was found that in the location block, if there is no break flag after rewrite or there is a last flag after rewrite, then the location block will be re-selected after rewrite. If the original location block is still entered, then the variable ($document_root, $fastcgi_script_name...) will not change, but if it is redirected to another location block after rewriting, the variables will change at this time

習慣沉默習慣沉默2746 days ago646

reply all(1)I'll reply

  • 習慣沉默

    習慣沉默2017-05-16 17:11:28

    If you don’t add a break, it will not stop and continue to find the next rewrite rule.

    Rewrite all requests to 404.html. If 404.html does not exist, it will be an infinite loop.

    reply
    0
  • Cancelreply