想在mac上寫一個 thinkphp5 的項目致份,用的 nginx 服務(wù)器,配置了一天各種500
础拨,404
氮块,not fond file
,活活的在虛擬主機上浪費了一天,如此诡宗,將配置貼出來分享給大家滔蝉。
$ php-fpm -v
PHP 7.0.15 (fpm-fcgi)
$ nginx -v
nginx version: nginx/1.10.3
虛擬主機配置:
server {
listen 80;
server_name www.tp5.com;
root /Users/mac/www/tp5/public;
index index.php index.html;
location / {
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php
{
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
}
參考鳥哥的博文:Nginx(PHP/fastcgi)的PATH_INFO問題
更新 : 2017-6-22
對于 2014 年后的 nginx 都已經(jīng)支持path_info 模式了。
新的配置
server {
listen 80;
server_name www.tp5.com;
set $root /Users/mac/www/tp5/public;
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root $root;
}
location / {
root $root;
index index.html index.php;
if ( -f $request_filename) {
break;
}
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
location ~ .+\.php($|/) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^((?U).+.php)(/?.+)$; # 支持path_info
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
include fastcgi_params;
}
}
參考:http://www.thinkphp.cn/topic/40391.html
還有一點
在引用 public/static
中的css塔沃,js和圖片的時候蝠引,不能使用/public/static/css/xxx.css
的地址,要去掉 public
用 /static/css/xxx.css
引用蛀柴,不然會報 404螃概,或者框架報 public 未找到
。