欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > ThinkPHP 3.2 + Nginx 页面404问题

ThinkPHP 3.2 + Nginx 页面404问题

2025/4/29 20:50:21 来源:https://blog.csdn.net/qq_42371336/article/details/142873887  浏览:    关键词:ThinkPHP 3.2 + Nginx 页面404问题

学习公司Callout项目时,发现公司项目所使用的TP版本是3.2,所以才可以使用例如,C,M,A等方法

因此我用phpEnv搭建了一个项目,域名为thinkphp,所选根目录如下

我打开网页,访问 thinkphp/ 和 thinkphp/index.php/Home/Index,有正常跳转页面,

实际访问的是application/Home/Controller/IndexController.class.php 的默认index 方法

根据php的url规则

thinkphp/index.php/Home/Index/index => 访问IndexController.class.php的index方法 (默认)

thinkphp/index.php/Home/Index/test => 访问IndexController.class.php的test方法

结果页面实际显示404,无法访问到控制器

这个原因如下:

引入

在使用Thinkphp的时候,项目在本地可以正常使用;但迁移到nginx后,会出现页面只能访问首页,其它页面出现404错误的问题。

这是因为TP默认采用的是pathinfo的URL访问模式(TP的URL访问模式可在手册中了解),而Nginx默认是没有开启pathinfo模式的,PHP默认也没有开启pathinfo的配置,所以访问时会出现错误。

pathinfo模式是怎样的模式呢?

例如:http://localhost/goodsLevel/index.php/Home/Index/index.html

上面的url就是采用pathinfo模式,特点如下:

1、路径中携带了入口文件index.php

2、在入口文件后跟随路径。格式一般为index.php/模块/控制器/方法/参数等

404页面出现原由

了解pathinfo模式后,我们就知道为什么Nginx环境下访问TP页面时出现404页面了。

因为按照常理来说,index.php后面跟随着路径,那么index.php就相当于一个文件夹了。但实际上又不存在index.php这个文件夹,所以如果没有开启pathinfo模式,那么这个路径解析就会报找不到文件。而只有在开启pathinfo模式之后,服务器才会去执行index.php这个文件。

解决方法:
修改nginx配置文件nginx.conf

phpEnv - 配置 - nginx - nginx.conf文件

#user  nobody;
worker_processes  1;error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;server_names_hash_bucket_size 512;client_header_buffer_size 32k;large_client_header_buffers 4 32k;client_max_body_size 100m;sendfile   on;#tcp_nopush on;keepalive_timeout 60;fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 256k;fastcgi_intercept_errors on;gzip on;gzip_min_length  1k;gzip_buffers     4 16k;gzip_http_version 1.1;gzip_comp_level 2;gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;gzip_vary on;gzip_proxied   expired no-cache no-store private auth;gzip_disable   "MSIE [1-6]\.";map $uri $path_info {~^(.+\.php)(.*)$ $2;}server {listen 80;server_name _;autoindex on;index index.html index.htm index.php;root D:/phpEnv/server/nginx/html/;access_log  logs/access.log;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;fastcgi_param  PATH_INFO        $path_info;fastcgi_param  PATH_TRANSLATED  $document_root$path_info;include        fastcgi_params;}location = /favicon.ico {log_not_found off;access_log off;}}include vhosts/*.conf;
}

加入以上配置后,重启即可

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词