nginx没有传递重写到php

时间:2013-11-18 19:32:34

标签: url-rewriting nginx fastcgi php

以下是我的nginx.conf文件的相关行。

location / {
    try_files $uri $uri/ /index.php @rewrite;
}

location @rewrite {
    rewrite ^/customer/(.*)$ /customersDisplay.php?id=$1;
    rewrite ^/attc2/(.*)$ /usr/www/vault/$1;
    rewrite ^/xport/(.*)$ /usr/www/files/innoMatrix/xport/$1;
    rewrite ^/forms/(.*)$ /usr/www/files/innoMatrix/forms/$1;
    rewrite ^/grafx/(.*)$ /usr/www/files/innoMatrix/grafx/$1;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param DATABASE innoMatrix;
    include fastcgi_params;
}

任何人都可以看到为什么客户不会被转移到fpm套接字?它重定向正确,但下载文件而不是在PHP中解释它。我使用几乎相同的配置为我的基于phalcon的应用程序,它是一个冠军。

1 个答案:

答案 0 :(得分:2)

我强烈建议您通过启用rewrite_log on来学习如何调试nginx重写。如果你这样做,你几乎肯定会发现正在发生的事情是:

  1. 请求'/ customer / foo'进来。
  2. 它与第一个位置块中的文件不匹配,因此请尝试通过@rewrite块。
  3. @rewrite块将请求重写为/customersDisplay.php?id=foo并重新开始处理请求。
  4. 第一个位置块现在尝试文件/customersDisplay.php并且它存在,因此它将作为文件提供。
  5. 尽可能温和地说,你编写nginx conf的方式是'反对nginx的常见做法',也就是说不要这样做。

    您可能正在迁移,或者过去曾使用过Apache重写,并且在Nginx中使用相同的重写方式。您几乎肯定不需要使用for for mapping for PHP。

    我建议,首先只需将文件fastcgi_params复制到fastcgi_php_params并在其中包含其他代理设置,(以避免重复)然后修改您的nginx配置,如下所示:

    #Mapping external URL to internal file path
    rewrite ^/attc2/(.*)$ /usr/www/vault/$1;
    rewrite ^/xport/(.*)$ /usr/www/files/innoMatrix/xport/$1;
    rewrite ^/forms/(.*)$ /usr/www/files/innoMatrix/forms/$1;
    rewrite ^/grafx/(.*)$ /usr/www/files/innoMatrix/grafx/$1;
    
    #All requests below 'customer' are fed to PHP
    location ~ /customer/(.*)$ {
        try_files $uri $uri/ /customersDisplay.php?id=$1 =404;
        include fastcgi_php_params;
    }
    
    #Try and serve all other static files directly, if they exist.
    location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ {
        try_files $uri /index.php?file=$1;
    
        #access_log off;
        expires 24h;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
    
    #final catch all location, pass all remaining requests to PHP.
    location / {
        try_files $uri $uri/ /index.php =404;
        include fastcgi_php_params;
    }