2

Nginx If Is Evil

 2 years ago
source link: https://fann.im/blog/2015/02/09/nginx-ifisevil/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Nginx If Is Evil

Feb 9, 2015

官方文档 IfIsEvil.

简单说在 location 中要尽量避免使用 if,如果一定要用,确保 if body 中只包含 return or rewrite,其他指令可能会出现莫名错误。

解决方案就是用 try_files 替换:

location / {
    try_files $uri $uri/index.html $uri.html =404;
}

如果想 if 和 try_files 一起用,可以把 if 放在 location 外:

set $APP 'unknown';
if ($query_string ~ "app=([^&]+)") {
   set $APP $1;
}
location = /api {
   try_files /$APP/data.json /data.json =404;
}

Nginx Pitfalls 列举了一些 nginx 陷阱,值得学习。


春哥这篇 How nginx “location if” works 做了逐步分析,学习。

Was this page helpful?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK