这是我的 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 的应用程序使用了几乎相同的配置,并且它运行得很好。
最佳答案
我强烈建议学习如何通过打开 rewrite_log on
来调试 nginx 重写。如果您这样做,您几乎肯定会看到正在发生的事情是:
- 收到对“/customer/foo”的请求。
- 它与第一个位置 block 中的文件不匹配,因此通过 @rewrite block 进行尝试。
- @rewrite block 将请求重写为
/customersDisplay.php?id=foo
并重新开始处理该请求。 - 第一个位置 block 现在尝试文件
/customersDisplay.php
并且它存在,因此它被用作文件。
尽可能温和地说,您编写 nginx conf 的方式“违反了 nginx 的常见做法”,也就是不要这样做。
您可能正在从 Apache Rewrite 迁移,或者过去使用过 Apache Rewrite,并且在 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;
}
关于url-rewriting - nginx 没有将重写传递给 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20056189/