使用PATH_INFO重写Nginx配置

时间:2015-05-03 22:07:44

标签: php apache .htaccess nginx rewrite

我在文件夹/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> 文件:

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

的index.php:

http://localhost/project/profile

当我打开这样的Path: /profile 网址时,我的index.php会显示:

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;
}

问题是我无法在Nginx中执行此操作。我试过这个:

http://localhost:8000/project/profile

但如果打开#include <iostream> #include <conio.h> using namespace std; int a[] = { 1,4,2,3,5,3,7}; int lung = sizeof(a) / sizeof(a[0]); int *l = new int[10]; void calc(int* a, int m){ a += 1; // Increment pointer for (int st = 0; st <m; st++){ l[st] = a[st-1]+a[st]+a[st+1]; } } int main(){ calc(a, lung-2); for (auto i = 0; i < lung-2; i++){ std::cout << l[i] << " "; } } 显示 404 Not Foud

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

2 个答案:

答案 0 :(得分:1)

使用lastrewrite中的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(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;#Fix PATH_INFO

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

答案 1 :(得分:0)

试试这个:http://winginx.com/en/htaccess

# nginx configuration
location / {
if (!-e $request_filename){
rewrite ^/([a-zA-Z0-9\-\/.]+)$ /index.php/$1 break;
}
}
location ~ *.php {
deny all;
}
location /index.php {
allow all;
}