nginx不会改写成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 socket吗? 它redirect正确,但下载文件,而不是在PHP解释。 我使用几乎相同的configuration为我的基于phalcon的应用程序,它的作品冠军。

我强烈建议学习如何通过打开rewrite_log on来调试nginx重写。 如果你这样做,你几乎肯定会看到发生的事情是:

  1. 请求“/ customer / foo”进来。
  2. 它不匹配第一个位置块中的文件,所以通过@rewrite块进行尝试。
  3. @rewrite块将请求重写到/customersDisplay.php?id=foo并重新开始处理请求。
  4. 第一个位置块现在尝试文件/customersDisplay.php ,它存在,所以它被作为文件。

把它写得尽可能温和,你写nginx conf的方式是“反对nginx的常用做法”,也就是说不要这样做。

你大概是从过去迁移过来的,或者过去用过Apache重写的,并且在Nginx中使用了相同的重写风格。 您几乎可以肯定不需要使用映射请求到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; }