Как получить PATH INFO в Nginx, когда мы используем rewrite (url дружелюбием к собакам)?


Я создал .htaccess в папке /var/www/project/:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond $1 !^(statics/([a-zA-Z0-9\-\/.]+)|index\.php)$ #ignora a pasta /statics
    RewriteRule ^([a-zA-Z0-9\-\/.]+)$ index.php/$1 [QSA,L] #Adiciona PATH_INFO
</IfModule>

<Files *.php>
    Order Deny,Allow
    Deny from all
</Files>

<Files index.php>
    Order Allow,Deny
    Allow from all
</Files>

Index.php:

<?php
echo 'Path: ', $_SERVER['PATH_INFO'];

Если доступ http://localhost/project/profile, index.php возвращает это:

Path: /profile

проблема заключается В попытке сделать это в Nginx. Я попытался это:

location ~ ^/project/(?!index\.php|statics/|data/)([a-zA-Z0-9\-\/.]+)$ {
    rewrite  ^(/project/)([a-zA-Z0-9\-\/.]+)$  $1/index.php/$2 break;
    return 500;
}

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

    #if (!-f $document_root$fastcgi_script_name) {
    #    return 404;
    #}

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

, Но когда я открываю http://localhost:8000/project/profile он показывает 404 Not Found.

Как я могу сделать nginx работает как .htaccess?

Author: Guilherme Nascimento, 2015-05-03

1 answers

Для работы необходимо использовать last rewrite и fastcgi_split_path_info, чтобы настроить PATH_INFO, например:

Примечание: Используйте полный путь в rewrite, и в location

location ~ ^/project/(?!index\.php/.*|index\.php$|statics/.*|data/.*)([a-zA-Z0-9\-\/.]+)$ {
    rewrite ^/project/(?!index\.php/.*|statics/.*|data/.*)([a-zA-Z0-9\-\/.]+)$ /project/index.php/$1 last;
}

location ~ ^/project/(?!index\.php).*\.php$ {
    deny all;
}

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

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
}
 1
Author: Guilherme Nascimento, 2020-12-20 22:31:05