如何首先configurationnginx到try_files,如果不存在请按照request_uri

我们有页面caching与ID分区和子域。 对于像ny.site.com/cat/12345la.site.com/dog/234这样的请求,nginx需要

  1. 检查文件/cat/ny/1234/5.html存在,将其返回
  2. 否则,只需使用原始的request_uri命中应用程序服务器,即可创buildcaching文件

我们设法找出了子域和id分区的一部分,但没有得到任何try_files部分运气。 在rewrite or internal redirection cycle while internally redirecting to总会遇到rewrite or internal redirection cycle while internally redirecting to等无限循环错误

我们的nginx.conf文件就像

 rewrite "/cat/([0-9]{4})([0-9]{1,4})" /system/cache/cat/$subdomain/$1/$2.html break; rewrite "/cat/([0-9]{1,4})" /system/cache/cat/$subdomain/$1.html break; try_files $uri $request_uri; 

任何提示? 谢谢!

更新:我尝试了以下。 Nginx正在寻找文件系统中的$ request_uri(例如,/ cat / 12345),而不是点击应用服务器。 有什么想法吗?

 try_files $uri @old; location @old { rewrite ^ $request_uri break; } 

试试这个

 location ^~ ^/cat/([0-9]{4})([0-9]{1,4}) { try_files "/cat/ny/$1/$2.html" @app_server; } location @app_server{ # pass this to your app server for processing. } 
  1. 使用^〜,因为这还包括/ cat / 12345 /(结尾斜线)等边缘情况。
  2. 只要确定是否需要$ uri(不包含查询字符串)或$ request_uri(包含查询字符串)。

我从来没有试过这个我的自我,但这是我如何写它

 location / { # or whatever location you see fit to your requirements try_files $uri.html $uri; # I don't know if the `.` needs to be escaped `$uri\.html`, you can try both }