tp5 项目在Apache环境下开发并运行正常,切换nginx环境后报404错误。
原因一:在Apache环境中设置了隐藏入口文件index.php,而nginx环境下未设置隐藏入口文件,当在nginx环境中时需要在地址栏中加入index.php后才可以访问。
原因二:项目中的入口文件原本是放在public目录下,但是入口文件让我改到了根目录下,在配置nginx.conf文件时需要将根目录地址改一下,如下:
server { location /{ root:'http://www.xxx.com/' } }
解决nginx 下隐藏入口文件问题:在nginx.conf文件中的localtion 下加入下面的代码:
if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; }
整体server代码
server { listen 80; server_name server_name; #监听地址 location / { root "完整的项目地址"; index index.html index.htm index.php; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=/$1 last; break; } } }