如何配置nginx以首先尝试\u文件,如果不存在,则遵循请求\u uri

如何配置nginx以首先尝试\u文件,如果不存在,则遵循请求\u uri,nginx,rewrite,page-caching,Nginx,Rewrite,Page Caching,我们有带有id分区和子域的页面缓存。比如说,对于像ny.site.com/cat/12345或la.site.com/dog/234这样的请求,nginx需要 检查文件/cat/ny/1234/5.html是否存在,并返回它 否则,只需使用原始的request\u uri点击应用服务器,就会创建缓存文件 我们设法找到了子域和id分区部分,但在try_文件部分没有找到任何运气。在内部重定向到 我们的nginx.conf文件如下 rewrite "/cat/([0-9]{4})([0-9]{1,4}

我们有带有id分区和子域的页面缓存。比如说,对于像
ny.site.com/cat/12345
la.site.com/dog/234
这样的请求,nginx需要

  • 检查文件
    /cat/ny/1234/5.html是否存在,并返回它
  • 否则,只需使用原始的
    request\u uri
    点击应用服务器,就会创建缓存文件
  • 我们设法找到了子域和id分区部分,但在try_文件部分没有找到任何运气。在内部重定向到

    我们的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 / { # 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
    }
    
    试试这个

    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.
    
    }
    
  • 使用^~因为这还包括像/cat/12345/(结束斜杠)这样的边大小写
  • 只需确定您想要的是$uri(没有查询字符串)还是$request\u uri (其中包含查询字符串)

  • 谢谢你的意见,但我不明白。这将如何解决这个问题?i、 例如,当磁盘上不存在$uri时,返回原始的$request\u uri?
    try\u files
    告诉nginx为该请求尝试什么,所以我只是告诉它看
    $uri
    是否有html文件,否则试试
    $uri
    ,如果您有一个处理请求的php文件,那么您应该将其作为第三个参数传递,即:
    /index.php$request\u uri
    forexample@surajs21:我有像
    www.test.com/public/
    www.test.com/public/web
    www.test.com/public/web1
    这样的url。这是ZF2,我面临的问题是正确获取第一个URL“public/”的链接,但问题是
    www.test.com/public/web
    www.test.com/public/web1
    。ngin x无法映射它。请帮帮我