How to configure nginx, Apache supports pathinfo mode

小云云
Release: 2023-03-21 15:52:02
Original
4384 people have browsed it

我也配置了很多次了,前面都快放弃了,最近在学习resuful框架写api,代码实现上没出现啥问题,倒是这个path_info的环境配置把我弄的比较郁闷你一定见过类似这样的路径:http://www.04007.cn/index.php/home/index/action/3408.html,对,这就是phpinfo模式。从上面可以看出其有以下几个特点。

1,URL中带有入口文件全名,即index.php,

2,在入口文件后面依然跟有路径比如上面的/home/index/action/3408.html

3,很多框架中后面分别是/模块/控制器/方法参数等,Thinkphp就是这样的。当然这个不一定非得是模块控制器方法的组成。

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

本人使用的是集成安装环境phpstudy,这个环境用的时间比较久

先检查这三个配置

一:
在PHP安装文件夹下找到PHP.INI,

;cgi.fix_pathinfo=1
改为
cgi.fix_pathinfo = On
保存,重启服务器即可
也有的说将
;cgi.fix_pathinfo=1
之前的 ; 去掉,即改为
cgi.fix_pathinfo=1
保存,重启服务器的。
但是我尝试发现,这样并不能解决问题,于是决定采用第二种方法;
二:
打开Apache安装文件夹下的conf文件夹,找到httpd.conf文件中找到下面这段代码

<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
AcceptPathInfo On    //加入这行代码就OK了
</Directory>
Copy after login

不幸的是,按照上面的方法,还是失败了。木办法,只能试下第三种方法了;
三:

在配置文件中加入

<Files *.php>
AcceptPathInfo On
</Files>
Copy after login

这样 Apache 就可以支持针对 php 文件的 PathInfo 了。
于是,又充满希望的试了一下,可惜,还是没有成功。
我想,这PathInfo跟URL有关,那会不会是因为Apache服务器没有开启mod_rewrite模块的关系呢?于是,我在Apache的配置文件(Apache安装目录的conf文件夹下的httpd.conf文件)里找到:
#LoadModule rewrite_module modules/mod_rewrite.so
将前面的#去掉,改为:
LoadModule rewrite_module modules/mod_rewrite.so

下面是nginx服务器开启phpinfo模式的方法:

location ~ [^/]\.php(/|$)
{
  # comment try_files $uri =404; to enable pathinfo
  #try_files $uri =404; #把这行代码注释,让服务器不尝试去加载路径文件
  fastcgi_pass unix:/tmp/php-cgi.sock;
  fastcgi_index index.php;
  include fastcgi.conf;
  include pathinfo.conf; #加载phpinfo.conf配置
}
Copy after login

重启nginx服务器即可。对于Apache服务器,需要先加载rewrite模块,将http.conf中的配置AllowOverride None将None改为 All,重启Apache即可。在.htaccess文件中加入

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
Copy after login

注:在配置虚拟域名的时候我的配置

我给个我的配置成功的php.ini和httpd.comf把,我在另外一台电脑没有成功的原因可能是我多加了个这个指向到项目地址的配置,例如我下面这个配置把

DocumentRoot  "E:\phpstudy\WWW\restful"
<Directory />
    Options +Indexes +FollowSymLinks +ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
    AcceptPathInfo On 
    Require all granted
</Directory>
Copy after login

指向到了项目单项目的根地址下把

我的配置的下载地址:

http://download.csdn.net/download/qq_27229113/9981419

最近在学习用restful框架写api,发现自己路由存在这个问题

我把虚拟域名指向为www.restfulapi.com/

虚拟域名地址指向为restfulapi这个文件夹

我的api目录写在restful/index.php里面,有两个方法,分别是users和articles两个方法

如果我输入http://www.restfulapi.com/restful/路由会发现还报这个错误Notice: Undefined index: PATH_INFO in E:\phpstudy\WWW\restfulapi\restful\index.php on line 107
{"error":"请求资源不被允许"}

但是输入这个路由时http://www.restfulapi.com/restful/users

便有数据反馈{"error":"用户名不能为空"}

在代码index.php打印加载的配置

var_dump($_SERVER);
Copy after login
反馈结果为
Copy after login
array(39) {    
["REDIRECT_STATUS"]=>    
string(3) "200"    
["HTTP_HOST"]=>    
string(18) "www.restfulapi.com"    
["HTTP_CONNECTION"]=>    
string(10) "keep-alive"    
["HTTP_CACHE_CONTROL"]=>    
string(9) "max-age=0"    
["HTTP_USER_AGENT"]=>    
string(108) "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36"    
["HTTP_UPGRADE_INSECURE_REQUESTS"]=>    
string(1) "1"    
["HTTP_ACCEPT"]=>    
string(85) "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"    
["HTTP_ACCEPT_ENCODING"]=>    
string(13) "gzip, deflate"    
["HTTP_ACCEPT_LANGUAGE"]=>    
string(14) "zh-CN,zh;q=0.8"    
["PATH"]=>    
string(272) "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\adb"    
["SystemRoot"]=>    
string(10) "C:\Windows"    
["COMSPEC"]=>    
string(27) "C:\Windows\system32\cmd.exe"    
["PATHEXT"]=>    
string(53) ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC"    
["WINDIR"]=>    
string(10) "C:\Windows"    
["SERVER_SIGNATURE"]=>    
string(0) ""    
["SERVER_SOFTWARE"]=>    
string(47) "Apache/2.4.23 (Win32) OpenSSL/1.0.2j PHP/5.5.38"    
["SERVER_NAME"]=>    
string(18) "www.restfulapi.com"    
["SERVER_ADDR"]=>    
string(9) "127.0.0.1"    
["SERVER_PORT"]=>    
string(2) "80"    
["REMOTE_ADDR"]=>    
string(9) "127.0.0.1"    
["DOCUMENT_ROOT"]=>    
string(26) "E:/phpstudy/WWW/restfulapi"    
["REQUEST_SCHEME"]=>    
string(4) "http"    
["CONTEXT_PREFIX"]=>    
string(0) ""    
["CONTEXT_DOCUMENT_ROOT"]=>    
string(26) "E:/phpstudy/WWW/restfulapi"    
["SERVER_ADMIN"]=>    
string(18) "admin@phpStudy.net"    
["SCRIPT_FILENAME"]=>    
string(44) "E:/phpstudy/WWW/restfulapi/restful/index.php"    
["REMOTE_PORT"]=>    
string(5) "59338"    
["REDIRECT_URL"]=>    
string(14) "/restful/users"    
["GATEWAY_INTERFACE"]=>    
string(7) "CGI/1.1"    
["SERVER_PROTOCOL"]=>    
string(8) "HTTP/1.1"    
["REQUEST_METHOD"]=>    
string(3) "GET"    
["QUERY_STRING"]=>    
string(0) ""    
["REQUEST_URI"]=>    
string(14) "/restful/users"    
["SCRIPT_NAME"]=>    
string(18) "/restful/index.php"    
["PATH_INFO"]=>    
string(6) "/users"    
["PATH_TRANSLATED"]=>    
string(32) "E:\phpstudy\WWW\restfulapi\users"    
["PHP_SELF"]=>    
string(24) "/restful/index.php/users"    
["REQUEST_TIME_FLOAT"]=>    
float(1505533005.973)    
["REQUEST_TIME"]=>    
int(1505533005)    
}    
{"error":"用户名不能为空"}
Copy after login
 发现 ["PATH_INFO"]=>string(6) "/users"
 说明这个path_info配置只指向到了restful/users和restful/articles目录下我怀疑是我的路由配置的原因在打开我的 
 .htaccess文件夹RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} 
 !-fRewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

还有我发现运行这个
http://www.restfulapi.com/restful/users路由的时候会经过二次跳转才访问到最终api,感觉这里存在问题
Copy after login

经过测试得出下面个人结论

在实际非api注册中的路由是www.restfulapi.com,可以完成用户注册,而我们使用resuful框架进行api
Copy after login
注册用户时的路由是www.restfulapi.com/restful/users和文章api www.restfulapi.com/restful/articles的路由既可以
Copy after login
是个人对路由理解不够透彻吧!总结下,上面的路由配置和php.ini和http.conf配置才是重点,完成好就可以实现php的pathinfo模式,
Copy after login

相关推荐:

有关pathinfo的文章推荐10篇

The above is the detailed content of How to configure nginx, Apache supports pathinfo mode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!