使用Nginx和PHP-FPM时,我得到一个400错误的请求错误,为什么?

我试图运行一个网站(这需要PHP – 目前在技术上不需要MySQL,但在不久的将来可能会有一段时间,因为我继续开发它,所以我继续安装,以及)使用nginx 1.2 .4和Ubuntu 12.04.1 LTS上的PHP-FPM 5.3.3。 据我所知,我没有做错任何事情,但显然有些事情是不正确的 – 每当我尝试浏览我的网站时,我似乎都会收到400错误的请求错误。 我主要是遵循一个指南,我已经做了或多或less的build议,除了没有设置PHP-FPM使用Unix套接字和我使用service而不是/etc/init.d/在启动时/停止nginx,PHP和MySQL。

无论如何,这里是我的相关configuration文件(我只有审查个人/敏感的细节,如我的域名 – 其中包含我的真实姓名):

/etc/nginx/nginx.conf

 user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 15; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; ## # nginx-naxsi config ## # Uncomment it if you installed nginx-naxsi ## #include /etc/nginx/naxsi_core.rules; ## # nginx-passenger config ## # Uncomment it if you installed nginx-passenger ## #passenger_root /usr; #passenger_ruby /usr/bin/ruby; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

/etc/nginx/sites-enabled/subdomain.mydomain.net

 server { listen 80; # listen for IPv4 listen [::]:80; # listen for IPv6 server_name www.subdomain.mydomain.net subdomain.mydomain.net; access_log /srv/www/subdomain.mydomain.net/logs/access.log; error_log /srv/www/subdomain.mydomain.net/logs/error.log; location / { root /srv/www/subdomain.mydomain.net/public; index index.php; } location ~ \.php$ { try_files $uri =400; include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /srv/www/subdomain.mydomain.net/public$fastcgi_script_name; } } 

上面的configuration文件中列出的所有目录在我的服务器上是正确的(就我所知)。

我没有在这篇文章中包含/etc/php5/fpm/pool.d/www.conf/etc/php5/fpm/php.ini ,因为它们相当长,但是我已经将它们发布到了Pastebin: http:/ /pastebin.com/ensErJD8和http://pastebin.com/T23dt7vM 。 尽pipe我在这两个文件中唯一改变的地方是在php.ini中,我将expose_php设置为off ,以隐藏用户的.php文件扩展名。

我能做些什么来解决我的问题? 请让我知道,如果我需要提供任何额外的细节。

这可能是一个错字:

  try_files $uri =400; 

它几乎可以肯定是:

  try_files $uri =404; 

我猜测TechZilla已经说过了,但是你可以修改这一行:

 fastcgi_pass 127.0.0.1:9000; 

是这个。

 fastcgi_pass unix:/var/run/php5-fpm.sock; 

我假设,因为你说你安装了PHP-FPM,使用.sock方法应该比通过TCP传递更好。

如果安装PHP-FPM的软件包也设置了必要的权限并正确configuration,则可以通过.sock方法连接到PHP-FPM。 否则,您可能不得不通过127.0.0.1:9000连接到PHP-FPM。 感谢保罗通知我。


这条线也是可能的:

 include fastcgi_params; 

可以改成这样:

 include fastcgi.conf; 

但是,不要指望我这个。 我不知道为什么这个工作在我的Ubuntu服务器虚拟机,但它现在做。


资料来源: https : //www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-ubuntu-14-04