想在mac上寫(xiě)一個(gè) thinkphp5 的項(xiàng)目,用的 nginx 服務(wù)器,配置了一天各種500,404,not fond file,活活的在虛擬主機(jī)上浪費(fèi)了一天,如此,將配置貼出來(lái)分享給大家。
$ php-fpm -v
PHP 7.0.15 (fpm-fcgi)
$ nginx -v
nginx version: nginx/1.10.3
虛擬主機(jī)配置:
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;
}
}
參考鳥(niǎo)哥的博文:Nginx(PHP/fastcgi)的PATH_INFO問(wèn)題
更新 : 2017-6-22
對(duì)于 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
還有一點(diǎn)
在引用 public/static 中的css,js和圖片的時(shí)候,不能使用/public/static/css/xxx.css 的地址,要去掉 public 用 /static/css/xxx.css 引用,不然會(huì)報(bào) 404,或者框架報(bào) public 未找到 。