要在Nginx中配置PATHINFO模式并隐藏ThinkPHP的index.php入口文件,需要进行以下配置:
server {
listen 80;
server_name yourdomain.com;
root /path/to/your/project/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# PATHINFO支持
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
server {
listen 80;
server_name yourdomain.com;
root /path/to/your/project/public;
index index.php index.html index.htm;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
# PATHINFO支持
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
确保ThinkPHP的URL模式配置正确(在config/app.php中):
'url_route_on' => true,
'url_route_must' => false,
确保项目根目录设置为public目录,而不是项目根目录
修改配置后需要重启Nginx:
sudo nginx -s reload
如果使用ThinkPHP6及以上版本,确保路由文件(route/app.php)中已定义相应路由
如果遇到404错误,请检查:
这两种方法都能实现隐藏index.php并支持PATHINFO模式,第一种方法性能更好,推荐使用。