使用nginx执行php脚本的麻烦

我的nginxconfiguration看起来像这样

server { listen 80; server_name localhost; location / { root /var/www; index index.php index.html; autoindex on; } location /folder1 { root /var/www/folder1; index index.php index.html index.htm; try_files $uri $uri/ index.php?$query_string; } location /folder2 { root /var/www/folder2; index index.php index.html index.htm; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } 

上述设置的问题是,我无法执行PHP文件。 现在根据我对nginxconfiguration规则的理解,当我在我的webroot( / )这是/var/www $document_root的值变成/var/www所以当我请求localhost/hi.phpfastcgi_param SCRIPT_FILENAME变成/var/www/hi.php ,这是PHP脚本的实际path。 但是,当我actully去的url localhost/hi.php我收到一条消息File not found. 但是,当我去的URL localhost/hi.html我可以看到页面的HTML输出。 所以这意味着php的位置块有问题。

同样,当我请求localhost/folder1/hi.php$document_root变为/var/www/folder1因为这被指定为folder位置块中的根,所以fastcgi_param SCRIPT_FILENAME变成/var/www/folder1/hi.php 。 但是当我actully去的URL localhost/folder1/hi.php我收到一条消息File not found. 但是,当我去的URL localhost/folder1/hi.html我可以看到页面的HTML输出。 所以再次PHP不被执行。

请帮忙?

您需要从两个location /folder*块中删除root行,并将root /var/www移动到server块内,而不是location /块。 这是最常见的nginx错误configuration之一 。