nginx支持yaf的path_info配置

本文详细介绍了如何解决YAF框架与Nginx服务器配合使用时出现的路径解析问题,通过调整Nginx配置文件中的rewrite规则及fastcgi_params参数,确保PHP-FPM模式下能正确处理请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按照yaf手册上面的nginx rewrite配置完成后,如果是用的nginx+php-fpm模式,一般情况下是解析不了的,不是404就是Access denied的提示,要不就是访问其他的页面的时候,地址是对的,返回结果却是主页(index)的。

今天配置的时候就出了这个问题,查了大部分资料都是说把

location / {
    if (!-e $request_filename){
        rewrite ^/(.*)$ /index.php/$1 last;
    }
}

改成

location / {
    if (!-e $request_filename){
        rewrite ^/(.*)$ /index.php?$1 last;
    }
}

我觉得这样不太科学啊,既然是path_info这上面的问题,那估计就得在nginx配置上找原因了。

通过对正则的调整,还有转发的规则调整,总算是搞定了。

fastcgi_params配置得足够,各种该带的一个也不能少,尤其是path_info这块的。

下面是 server 的配置

    server {
        listen       80;
        server_name  nk.yaf.com;
        root   /Users/xingkaroc/mywww/yaf;
        index  index.html index.php;
        access_log  logs/nkyafcom.access.log  main;
        error_log  logs/nkyafcom.error.log  info;

        #if (-f $request_filename) {
        #    break;
        #}
        location / {
            if (!-e $request_filename){
                rewrite ^/(.*)$ /index.php/$1 last;
            }
        }
        location ~* \.php {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            fastcgi_param HTTP_PROXY "";
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            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  PATH_INFO          $fastcgi_path_info;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
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  HTTPS              $https if_not_empty;

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-redirect
fastcgi_param  REDIRECT_STATUS    200;

最后就是,PHP里面,php.ini需要开启 cgi.fix_pathinfo 这个参数

cgi.fix_pathinfo=1

按照这样配置,yaf就能正常使用了。

以下是我项目的入口文件: <?php // echo phpinfo(); // die; /** * @name index.php * @desc 入口文件 * @author boye.liu */ try { // Define path to application directory defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application')); // Ensure /library on include_path, // And library must be the first, to avoid the error: "Fatal error: Cannot redeclare class Zend_Db" set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/library'), get_include_path(), ))); // Create application, bootstrap, and run $application = new Yaf_Application( APPLICATION_PATH . '/configs/application.ini' ); $application->bootstrap() ->run(); } catch (Exception $ex) { header("Content-Type: application/json;charset=utf-8"); $code = $ex->getCode(); $msg = $ex->getMessage(); Kepler_Log::error($msg); $errArr = array( "errno" => 1, //system error "errmsg" => "系统错误", "data" => array(), ); echo json_encode($errArr); } 以下时application.ini文件: [product] ;=========== php ini setup phpSettings.display_startup_errors = 0 phpSettings.display_errors = 0 phpSettings.date.timezone = "Asia/Shanghai" ;phpSettings.error_reporting = "E_ALL & ~ E_STRICT" ;=========== application application.directory = APPLICATION_PATH "/" application.bootstrap = APPLICATION_PATH "/Bootstrap.php" application.library = APPLICATION_PATH "/library" application.library.namespace = "Zend,Kepler" ;======= 忽略前缀,仅仅当用于分产品线才使用 ;application.baseUri = "/audit" application.dispatcher.throwException=True application.dispatcher.catchException=True application.dispatcher.defaultModule = "index" application.dispatcher.defaultController = "index" application.dispatcher.defaultAction = "index" ;======= 开发添加的module需要添加到这里 application.modules = "Index,Audit,User,Common,Collect,Business,Data,Market,Company,Shop,Goods,Enquiry,Offer,Order,News,Dt,Meeting,Scf,Crm,Journal,Report,Article,Vote" ;======= application.system.* 通过这个属性, 可以修改yaf的runtime configure ;======= use_spl_autoload must be true application.system.use_spl_autoload=1 为啥现在我所有的接口地址都报错404,但是不带接口的话也能进入到入口文件中去?
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值