如何正確配置 Nginx+PHP
假設我們用PHP實現了一個前端控制器,或者直白點說就是統一入口:把PHP請求都發送到同一個文件上,然後在此文件裏通過解析「REQUEST_URI」實現路由。
一般這樣配置
-
server {
-
listen 80;
-
server_name foo.com;
-
-
root /path;
-
-
location / {
-
index index.html index.htm index.php;
-
-
if (!-e $request_filename) {
-
rewrite . /index.php last;
-
}
-
}
-
-
location ~ \.php$ {
-
include fastcgi_params;
-
fastcgi_param SCRIPT_FILENAME /path$fastcgi_script_name;
-
fastcgi_pass 127.0.0.1:9000;
-
fastcgi_index index.php;
-
}
-
}
這裏面有很多錯誤,或者說至少是壞味道的地方,大家看看能發現幾個。
Nginx配置文件分爲好多塊,常見的從外到內依次是「http」、「server」、「location」等等,缺省的繼承關係是從外到內,也就是說內層塊會自動獲取外層塊的值作爲缺省值。