我正在设置一个使用nginx的开发环境,并使用fastcgi将请求转发到命令行php服务器。 当我直接向PHP服务器发送请求时,它会正确处理它们。 nginx服务器可以正确处理非php文件的请求。 但是,当我通过nginx发送一个PHP请求时,PHP服务器被命中,但返回“无效的请求(格式错误的HTTP请求)”。
抱歉连接所有的configuration文件,但我不知道从哪里开始。 所有文件都位于$PROJECT_PATH/dev-config/
。
这里是我的nginxconfiguration的相关部分。
http { ... server { root .; listen 8123; server_name localhost; location / { index index.php; } location ~ \.php$ { try_files $fastcgi_script_name =404; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass 127.0.0.1:9123; fastcgi_index index.php; include fastcgi_params; # this file is the default } } }
这里是运行到(重新)启动nginx和php的脚本:
#!/bin/bash PROJECT_PATH="$(dirname $0)/../" nginx -s stop || true nginx -c dev-config/nginx.conf -p $PROJECT_PATH php -c php-dev.ini -t $PROJECT_PATH -S 127.0.0.1:9123
我发送的文件是test.php:
<? echo 'YAY!' ?>
在我的nginx错误日志里面,我收到了这样的消息:
2017/10/27 08:12:56 [error] 68934#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9123", host: "localhost:8123"
同时,PHP正在发送这样的消息:
[Fri Oct 27 08:12:56 2017] 127.0.0.1:52470 Invalid request (Malformed HTTP request)
这表明请求是由nginx处理,然后发送到php服务器。 但是这个请求有些不正确。
要清楚的是,这意味着在笔记本电脑上的开发环境中运行,不需要为每个用户更改configuration。
我遇到的问题是,命令行PHP服务器没有与它运行fastcgi。 由于命令行服务器已经是服务器,我根本不需要fastcgi。 相反,我可以proxy_pass
它。
我改变了这个PHP的位置块,它的工作原理:
location ~ \.php$ { proxy_pass http://127.0.0.1:9123; }