用nginx配置thinkphp5.1框架项目网站的虚拟主机后,服务启动正常,访问首页没问题,但是访问指定路由路径URL时,出现页面404问题。
解决办法
在nginx配置文件或自定义虚拟主机配置文件中添加这句代码:
注:在Linux系统下:
try_files $uri $uri/ /index.php?$query_string;注:在window系统下:
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/$1 last;
break;
}详细配置如下(可参考)
server {
listen 80;
server_name yourdomain.com;
root /path/to/your/project/public; # ThinkPHP的public目录通常是存放静态文件的目录
location / {
# 如果你的系统是Linux,那么请添加这句话:
try_files $uri $uri/ /index.php?$query_string;
# 如果你的系统是Windows,那么请添加这段话:
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本和配置修改
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_VALUE "upload_max_filesize=100M";
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}