在Windows7上安裝了Nginx+PHP,參考教程為 https://www.cnblogs.com/anlia/p/5916758.html
啟動 nginx 后,在瀏覽器中輸入localhost可以看到成功訪問nginx的頁面:

在html目錄中添加了一個phpinfo.php文件,內容為:
<?php
phpinfo();
?>
在瀏覽器中輸入:
localhost:/phpinfo.php
頁面卻顯示:

后來意識到是 nginx 安裝目錄下的 conf/nginx.conf 文件配置不正確:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
如上配置中的 SCRIPT_FILENAME , /scripts 是不存在的路徑,應當更改為存放 .php 文件的目錄路徑:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME D:/nginx-1.15.3/html/$fastcgi_script_name;
include fastcgi_params;
}
或者使用 $document_root 來代替,該變量即為 location 配置塊中的 root 指定的目錄 ‘html’。
反思總結
回頭再看那篇參考教程,里面的配置就是把 SCRIPT_FILENAME 寫成 '$document_root/$fastcgi_script_name' (原文中 $document_root 后面少了一個 '/'),當時因為在配置文件中沒有找到 $document_root 的定義,所以就沒放在心上,以為是各自的環境不一樣的原因,現在看來,$document_root 是 nginx 的一個內置環境變量,專門用來指代當前 location 配置塊中的 root 變量。
補充:
剛才試驗了一下,發現
$document_root/$fastcgi_script_name (有'/')
和
$document_root$fastcgi_script_name (無'/')
都可以使 nginx 正常找到 .php 文件
