我有一个nginx重写的麻烦。 我想重写^(.*?)/(.*?)/?$到controllers/$1.php?action=$2 。 这在早些时候在Apache服务器上工作。 之后,我想把这个文件路由到php-fastcgi。
对于一个正常的请求(即当我input完整path,而不使用重写),php-fastcgi正常工作。
我的configuration:
server { listen [::]:80; root /var/www/my-dir; index index.php index.html index.htm; charset utf-8; server_name my-domain; location / { autoindex off; rewrite ^(.*?)/(.*?)/?$ controllers/$1.php?action=$2 last; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } location ~ /\. { deny all; } }
编辑完这个configuration之后,我做了一个service nginx restart ,当然。
由于某些原因,重写后不使用php-fastcgi的位置块。 为什么这个&如何解决它?
相反,我得到一个“没有指定的input文件”作为回应。 在错误日志中,我看到:
2013/06/01 19:00:25 [error] 14288#0: *1 access forbidden by rule, client: xxx, server: my-domain, request: "GET /user/create HTTP/1.1", host: "my-domain"
你错过了一个try_files 。
location / { try_files $uri $uri/ /index.php; # ....everything else }
你也可能有一个rewrite的问题,因为你没有/在相对的URL controllers/$1.php... 。 尝试改变/controllers/$1.php...
而且,你错过了一个fastcgi参数,告诉php-fpm在哪里可以findPHP脚本。 将此添加到location ~ \.php$块的location ~ \.php$ 。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;