我有以下的nginxconfiguration:
location / { try_files $uri $uri/ index.html =404; if (!-e $request_filename) { rewrite ^/(.+)$ index.php?url=$1 last; } } location ~ .php$ { # protection from known vulnerability fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
( fastcgi_params是Debian软件包的默认设置)
它适用于请求/ ,但是当请求被重写时,主文件没有find:
请求/contact应该被重写为/index.php?url=contact
*104 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.0.0.1, server: localhost, request: "GET /contact HTTP/1.0", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "localhost:8080"
我无法从日志中得到什么是fastcgi试图加载,哪个path?
请注意, index.php和/index.php是不同的URI。 你在重写时忘记了斜杠。
这是实现相同function的更好方法:
location / { try_files $uri $uri/ @rewrite; } location @rewrite { rewrite ^/(.+)$ /index.php?url=$1 last; }
然后你的PHP位置块在你的设置。
try_files首先检查与$ uri匹配的文件是否存在,然后检查目录,如果两者都不存在,则使用运行脚本的rewrite -location。
最有可能的原因是您的设置无法正常工作,从重写脚本path丢失。 无论如何,这个设置比较简单是nginx的首选。