用PATH_INFO重写Nginx配置 [英] Nginx config rewrite with PATH_INFO

查看:70
本文介绍了用PATH_INFO重写Nginx配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件夹/var/www/project/中创建 .htaccess 文件:

I create .htaccess file in folder /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)$ #ignore folder statics
    RewriteRule ^([a-zA-Z0-9\-\/.]+)$ index.php/$1 [QSA,L] #Add path_info
</IfModule>

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

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

index.php:

index.php:

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

当我打开像 http://localhost/project/profile 这样的网址时,我的index.php显示如下:

When I open a url like this http://localhost/project/profile, my index.php show this:

Path: /profile

问题是我无法在Nginx中执行此操作.我试过了:

The problem is that I am unable to do this in Nginx. I tried this:

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 Foud .

But if open http://localhost:8000/project/profile show 404 Not Foud.

我如何才能使Nginx的功能与.htaccess相同?

How can I Nginx function identically to .htaccess?

推荐答案

rewrite fastcgi_split_path_info 中使用 last 修复PATH_INFO ,例如:

Use last in rewrite and fastcgi_split_path_info for fix the PATH_INFO, eg.:

注意: 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(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;#Fix PATH_INFO

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

这篇关于用PATH_INFO重写Nginx配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆