【问题标题】:Use both rewrite and php-fpm for .php extension in nginx在 nginx 中同时使用 rewrite 和 php-fpm 作为 .php 扩展名
【发布时间】:2016-08-30 01:44:53
【问题描述】:

我正在尝试从 Apache + mod_php 迁移到 Nginx + PHP-FPM。我目前正在使用mod_rewrite 将一些以.php 结尾的虚拟URI 重写为实际的PHP 脚本。这在使用mod_php 时非常有效。但是使用Nginx + FPM,因为我们必须使用proxy_pass,所以这是行不通的。当我添加一个正则表达式位置块以匹配 .php 扩展名时,它获得最高优先级,并且我的重写规则不适用。

我该如何解决这个问题?

location /test/ {
    rewrite "^/test/([a-z]+).php$" test.php?q=$1 last; 
}

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;

    set $fastcgi_script_name_custom $fastcgi_script_name;
    if (!-f $document_root$fastcgi_script_name) {
        set $fastcgi_script_name_custom "/cms/index.php";
    }

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}

【问题讨论】:

    标签: php nginx url-rewriting


    【解决方案1】:

    您可以将rewrite 规则置于location 块上方的server 块上下文中。或者,您可以将 rewrite 规则放在与 URI 匹配的 location 块中。

    所以你可以使用这个:

    rewrite "^/test/([a-z]+).php$" /test.php?q=$1 last;
    
    location / { ... }
    
    location ~ [^/]\.php(/|$) { ... }
    

    或者这个:

    location / { ... }
    
    location ~ [^/]\.php(/|$) {
        rewrite "^/test/([a-z]+).php$" /test.php?q=$1 break;
        ...
    }
    

    请注意,重写后的 URI 需要一个前导 /(与 Apache 约定不同)。

    详情请见this document

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-04
      • 2013-10-23
      • 2016-11-03
      • 1970-01-01
      • 2013-04-08
      • 2016-03-06
      • 2019-11-27
      • 1970-01-01
      相关资源
      最近更新 更多