admin 发表于 2020-11-22 14:47:47

原创图解:ThinkPHP Nginx配置设置隐藏index.php,缩短路径正确...

<p>一、在ThinkPHP的开发手册中,有这样一段说明 :</p><p>隐藏index.php,可以去掉URL地址里面的入口文件index.php ,但是需要额外配置WEB服务器的重写规则。</p><p>如果是Nginx 环境的话,可以在Nginx.conf 中添加:</p><p>location / {&nbsp;<br></p><p>if (!-e $request_filename) {</p><p>rewrite ^(.*)$ /index.php?s=/$1 last;</p><p>break;</p><p>}</p><p>}</p><p>这段说明写的过于简单,而大家的实际应用环境千差万别,只按这一段话去配置的话,除非是最低级的新手可能会成功,为什么呢,因为只有最低级的新手才会有以上的Nginx环境,开发手册的说的就是在最简单只简单安装Nginx并且只有一个根目录网站的情况去配置Nginx.conf 的情况。但实际上我想学PHP的人没有只在根目录下直接建网站的,所以肯定不会成功。</p><p>你如果按开发手册配置最容易出现的错误是nginx: "location" directive is not allowed here in 。这是因为你的网站不只一个,并且目录并不是根目录,所以根本不能配置Nginx.conf这个文件,这个文件只有你的服务器只有一个网站且直接在根目录下时才有效。</p><p><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">尊重知识产权,转载请注明并复制本段,一元复始技术论坛原创</span><a href="https://www.1rmb.net/" target="_blank" title="一元复始技术论坛" style="font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">http://www.1rmb.net</a><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">.</span><br></p><p>二、以下是在PHPSTUDY环境多网站模式下Ngnix的正确配置方法。</p><p><img _moz_resizing="false" src="https://www.1rmb.net/upload/tid/20/53a8d0b98796483b129854e6b2df2198.jpg"><br></p><p>在phpstudy多网站模式下,需要配置的是VHOST.CONF文件,根本不是什么Nginx.CONF文件,要不然你怎么也不会配置成功。APACHE同理一定要到你的虚拟网站的配置文件和目录里配置才会起作用。</p><p><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">尊重知识产权,转载请注明并复制本段,一元复始技术论坛原创</span><a href="https://www.1rmb.net/" target="_blank" title="一元复始技术论坛" style="font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">http://www.1rmb.net</a><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">.</span><br></p><p>三、vhost.conf文件的配置。</p><p>如果你直接把开发手册中的那段代码粘贴进去,会提示如下错误:</p><p>nginx: duplicate location "/" in。<br></p><p>这时因为在VHOST.CONF文件里已经有&nbsp; &nbsp;location / { }</p><p><br></p><p>所以你只需要把</p><p>if (!-e $request_filename) {</p><p>rewrite ^(.*)$ /index.php?s=/$1 last;</p><p>break;</p><p>}</p><p>这一段文件复制到原有的location / { }中去即可。</p><p><br></p><p>配置完成后的文件如下图。&nbsp; 配置完成记得重启nginx.</p><p><img _moz_resizing="false" src="https://www.1rmb.net/upload/tid/20/9afaa47fd8aef1133ed98779f4f0761e.png"><br></p><p>好了,以上才是PHPSTUDY,nginx多网站模式下的正确配置THINKPHP隐藏INDEX.PHP文件的配置方法,如果你按照官方开发手册不会配置成功,开发手册中的坑是真多啊,光填这些坑就要浪费好多学习者的时间。所以总结如上,希望大家少走弯路。</p><p><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">尊重知识产权,转载请注明并复制本段,一元复始技术论坛原创</span><a href="https://www.1rmb.net/" target="_blank" title="一元复始技术论坛" style="font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">http://www.1rmb.net</a><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">.</span><br></p><p><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;"><br></span></p><p><span style="color: rgb(51, 51, 51); font-family: &quot;noto sans cjk sc&quot;, &quot;pingfang sc&quot;, &quot;microsoft yahei&quot;, &quot;hiragino sans gb&quot;, sans-serif;">如果对你有帮助,请点赞或留言。</span></p>
页: [1]
查看完整版本: 原创图解:ThinkPHP Nginx配置设置隐藏index.php,缩短路径正确...