作为新手的我们配置服务器一个都会选用LNMP一键安装包。
在安装lnmp一键安装的时候,一般都没有开启访问pathinfo模式
这种模式在比较多的框架中用到,比如的ThinkPHP,今天小风摸索了大半天,总算是找到了方法
这个是lnmp.org(我装的就是这个,所以我以这个为例子)
开出来的虚拟机的配置中的一段(vhost/***.conf),找到对应的网站的程序
修改nginx配置文件也是这样修改
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
优快云找到资料:
大家明显看到,location中的正则中,写了一个$,悲剧了,表示就此结束(?表示前面的php只匹配一次,这个只是随便提提)
最终,应该如此处理(location哪里也修改了,因为我没有用到php5处理):
location ~ \.php
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
参考的网络上搜索到的文档,带注释完整 版告诉大家:(复制到配置文件里面修改下,域名 和网站目录 lnmp 1.1里面就可以用了)
server
{
listen 80;
#注意修改域名为你的网站域名;
server_name yourname.com;
index index.html index.htm index.php ;
#注意修改 网站存放目录 为你的网站对应目录
root /home/wwwroot/webdir;
location ~ \.php
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#初始化一个变量
set $path_info "";
#初始化一个变量,并且获取到一个原始赋值
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#检测如果.php后面还存在/开始参数,将参数处理
set $real_script_name $1;#将第一个正则子串匹配到的赋值
set $path_info $2;#将第二个正则子串匹配到的赋值
}
#修改SCRIPT_FILENAME值
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
#修改SCRIPT_NAME值
fastcgi_param SCRIPT_NAME $real_script_name;
#修改PATH_INFO值
fastcgi_param PATH_INFO $path_info;
#上述三个赋值都是replace into的模式,这些值都是写在fastcgi.conf中
}
location / {
#Rewrite启用;
if (!-e $request_filename)
{
rewrite ^\/(.*)$ /index.php/$1 last;
break;
}
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log off;
}
最后,还要注意php.ini中的cgi.fix_pathinfo=0要注释掉(即在前面加上分号;),然后重启php-fpm和nginx