将TP5项目转移到服务器上后,因为TP的入口文件在public文件夹中,所以需要将网站的根目录也设置在public文件夹下,具体操作在/usr/local/nginx/conf/nignx.conf配置文件中。
随后问题出现了,可以访问项目的入口文件但访问控制器和方法提示404找不到。
去官网了解之后得知LNMP环境是默认不支持pathinfo模式的,需要自己在nignx.conf配置文件中修改设置。
将include enable-php.conf;替换为include enable-php-pathinfo.conf;
enable-php-pathinfo.conf内容
location ~ [^/]\.php(/|$)
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
重启nginx服务后生效。
这样配置后,通过 http://serverName/index.php/模块/控制器名/方法名 来访问,为了使URL更简洁,需要去掉入口文件
上面我们是通过引入enable-php-pathinfo.conf 这个配置文件来实现的pathinfo模式,而thinkphp官网在手册中为我们提供了隐藏index.php入口文件的方法
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
将这段代码,复制到enable-php-pathinfo.conf 里面
location ~ [^/]\.php(/|$)
{
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
}
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
重启lnmp服务生效,通过
http://serverName/模块/控制器名/方法名
来访问如果你不喜欢这种配置方法,可以参考:https://blog.csdn.net/woshihaiyong168/article/details/54973353