您的位置:首页 > 运维架构 > Nginx

thinkphp 两种方法访问 nginx

2016-06-13 13:47 471 查看
方法一:

使用pathinfo方式

访问 http://test.123.com/index.php/Lend/index.html
不隐藏index.php 直接访问内容

第一步 修改nginx.conf配置

server {

listen 80;

server_name test.123.com;

access_log /logs/devwww.123bianlidai.com_nginx.log combined;

index index.html index.htm index.php;

root /www/m.123bianlidai.com/;

location ~ \.php {

        #fastcgi_pass remote_php_ip:9000;

    fastcgi_pass unix:/dev/shm/php-cgi.sock;

    fastcgi_index index.php;

    include fastcgi_params;

    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;
#注释的两个参数是老版本使用的pathinfo方式

   # fastcgi_param SCRIPT_NAME $real_script_name;

   # fastcgi_param PATH_INFO $path_info;

     fastcgi_split_path_info ^((?U).+.php)(/?.+)$;

  fastcgi_param PATH_INFO $fastcgi_path_info;

  fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

    }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {

    expires 30d;

    access_log off;

    }

location ~ .*\.(js|css)?$ {

    expires 7d;

    access_log off;

    }
}

第二步修改php.ini配置

cgi.fix_pathinfo=1

在这之前还要介绍一个php.ini中的配置参数cgi.fix_pathinfo,它是用来对设置cgi模式下为php是否提供绝对路径信息或PATH_INFO信息。没有这个参数之前PHP设置绝对路径PATH_TRANSLATED的值为SCRIPT_FILENAME,没有PATH_INFO值。设置这个参数为cgi.fix_pathinfo=1后,cgi设置完整的路径信息PATH_TRANSLATED的值为SCRIPT_FILENAME,并且设置PATH_INFO信息;如果设为cgi.fix_pathinfo=0则只设置绝对路径PATH_TRANSLATED的值为SCRIPT_FILENAME。cgi.fix_pathinfo的默认值是1。

nginx默认是不会设置PATH_INFO环境变量的的值,需要php使用cgi.fix_pathinfo=1来完成路径信息的获取,但同时会带来安全隐患,需要把cgi.fix_pathinfo=0设置为0,这样php就获取不到PATH_INFO信息,那些依赖PATH_INFO进行URL美化的程序就失效了。

方法二:

使用tp官网的伪静态方法。不使用pathinfo 使用nginx  rewrite

隐藏index.php

访问URL地址:http://test.123.com/Lend/index.html

第一步修改nginx配置

location / {

    if (!-e $request_filename) {

        rewrite ^(.*)$ /index.php?s=$1 last;

        break;

    }

}

location ~ \.php {

    fastcgi_pass unix:/dev/shm/php-cgi.sock;

    fastcgi_index index.php;

    include fastcgi_params;

    set $real_script_name $fastcgi_script_name;

        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {

        set $real_script_name $1;

        }

    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;

    fastcgi_param SCRIPT_NAME $real_script_name;

    }

第二步找到tp app中的配置文件

把u_model方法改成2

/path/to/Application/Common/Conf/config.php

添加最后一段

<?php

return array(

        //'配置项'=>'配置值'

    //设置Smarty模板引起使用

    // 'TMPL_ENGINE_TYPE'      =>  'Smarty',     // 默认模板引擎

    //为Smarty配置相关配置

    // 'TMPL_ENGINE_CONFIG' => array(

    //     'left_delimiter' => '<{',

    //     'right_delimiter' => '}>',

    // ),

     'URL_MODEL'             =>  2,

);

参考资料:
http://doc.thinkphp.cn/manual/_search.html http://www.thinkphp.cn/topic/23164.html
http://www.nginx.cn/426.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx thinkphp