nginx配置 thinkphp需要的pathinfo

来源:互联网 发布:数据库怎么设置外键 编辑:程序博客网 时间:2024/04/16 12:33

由于nginx对pathinfo的支持不是很友好,所以原本执行正常的thinkphp程序放到运行着nginx的服务器上就瞎火了。

由于nginx只支持fastcgi的方法连接php,所以服务器必须先安装好php-fpm并运行起来。

nginx的配置


   location ~ [^/]\.php(/|$) {
        root           /var/www/html;

       #转交给php-fpm程序处理
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        #fastcgi_split_path_info的作用是使用正则匹配出pathinfo放在$fastcgi_path_info变量里面
         fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        fastcgi_param   PATH_INFO               $fastcgi_path_info;
        include        fastcgi_params;
    }

fastcgi_params文件的内容列表为

fastcgi_param  QUERY_STRING       $query_string;fastcgi_param  REQUEST_METHOD     $request_method;fastcgi_param  CONTENT_TYPE       $content_type;fastcgi_param  CONTENT_LENGTH     $content_length;fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;fastcgi_param  REQUEST_URI        $request_uri;fastcgi_param  DOCUMENT_URI       $document_uri;fastcgi_param  DOCUMENT_ROOT      $document_root;fastcgi_param  SERVER_PROTOCOL    $server_protocol;fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;fastcgi_param  REMOTE_ADDR        $remote_addr;fastcgi_param  REMOTE_PORT        $remote_port;fastcgi_param  SERVER_ADDR        $server_addr;fastcgi_param  SERVER_PORT        $server_port;fastcgi_param  SERVER_NAME        $server_name;# PHP only, required if PHP was built with --enable-force-cgi-redirectfastcgi_param  REDIRECT_STATUS    200;


如果需要在访问的时候在url里面省去index.php的话需要做rewrite

    if (!-e $request_filename)    {            #地址作为将参数rewrite到index.php上。            rewrite ^/(.*)$ /index.php/$1;            #若是子目录则使用下面这句,将subdir改成目录名称即可。            #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;    }

把这个写在

location / {
        #写在这里
            }

rewrite还可以简化为

location / {#其他原有的所有配置        try_files $uri  /index.php/$uri;        #若是子目录try_files $uri  /子目录/index.php/$uri;            }
try_files的官方指令说明描述了详尽的配置,比如wordpress等,可以拿来参考


ps:nginx的echo模块很适合调试nginx配置 

ps:还发现一个诡异的问题,代码完全没问题就是提示mysql连接失败,现象是使用mysqli连接数据库提示无法连接(apache环境下),但是单独摘出来连接代码缺可以连接(在apache环境下),后来才发现是selinux搞的鬼,有兴趣的可以查看一下 httpd_can_network_connect 这个se的bool值,默认是关的,这也是为啥放在nginx环境下却可以连接的原因,因为selinux的targeted规则只限制了apache的访问。

解决方法是关掉selinux或者设置httpd_can_network_connect这个bool为on

参考文章

http://www.thinkphp.cn/topic/3138.html

http://blog.csdn.net/xiangliangyu/article/details/24292827

http://wiki.nginx.org/PHPFcgiExample

https://github.com/openresty/echo-nginx-module     nginx的echo模块


0 0
原创粉丝点击