Maybe a shot in the dark: "But if these directives are put inside the “ ... ” location, the last flag should be replaced by break, or otherwise nginx will make 10 cycles and return the 500 error: "
>From this: [1] [2]. [1] http://nginx.org/en/docs/http/ngx_http_rewrite_module.html [2] https://www.nginx.com/blog/creating-nginx-rewrite-rules/

