【问题标题】:Rewrite all requests to index.php, except /static/ directory重写所有对 index.php 的请求,除了 /static/ 目录
【发布时间】:2016-07-13 05:23:43
【问题描述】:

这是我的 nginx.conf:

if ($host ~ "^example.com$") {
    rewrite . /index.php last;
}

如何让 nginx 在不重写 url 的情况下提供 /static/ 目录中的文件?

【问题讨论】:

    标签: regex .htaccess nginx rewrite nginx-location


    【解决方案1】:

    不确定为什么要测试$host 的值。区分主机名的常用方法是使用多个服务器块。详情请见this document

    try_files 指令用于首先检查静态文件是否存在,如果不存在则执行默认操作:

    server {
        ...
    
        root ...;
    
        location / {
            try_files $uri $uri/ /index.php;
        }
    
        location ~ \.php$ { ... }
    }
    

    这适用于任何静态文件(不仅仅是/static 目录中的那些)。详情请见this document

    但是,如果您特别希望以不同方式处理 /static 目录,请使用带有 ^~ 修饰符的前缀位置:

    location ^~ /static { }
    

    详情请见this document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      相关资源
      最近更新 更多