rryqszq4 / ngx-php

ngx-php - Embedded php7 or php8 scripting language for nginx module. Mainline development version of the ngx-php.
BSD 2-Clause "Simplified" License
582 stars 56 forks source link

Good work, how and where to use? #108

Open betrixed opened 3 years ago

betrixed commented 3 years ago

I followed this because of noting results on Techempower benchmarks #19. A link to the benchmark code would be nice.

Following instructions, I had success with this, on setting up nginx 1.19.0 and php 7.4.7, and running a few local php scripts from browser. I have callled a few of the ngx_php7 API. I note that the static class method call versions did not work.

On my system, nginx compiled the ngx_php addon directly into the nginx binary.

On testing some scripts, I found. API ngx_request::server_protocol(void) : string produced an error, but ngx_request_server_protocol(void) : string worked OK.

I make a guess here, and ask to confirm. Is it true that PHP does automatically write back to the open socket stream connection given by nginx as default?

I note that require once only works for one invocation, after which all classes remain defined. It is similar to the new php code preload facility which I haven't yet made to work.

Too many PHP frameworks that I know all make some anchoring usage of class static variable, instance singleton, or global.

rryqszq4 commented 3 years ago

@betrixed Thanks for your feedback !

First question, 'ngx_request::server_protocol' is an older API, but it is nothing different compare with 'ngx_request_server_protocol', and need some error details.

Secode question that ngx_php is per worker one php virtual machine and all of class must be defined only once .

About last question, if not use non-block API and just need to adapted SAPI can be work run. For example (master of branch):

class NgxphpDriver
{
    public static $server = null;

    public static $requestHandleRunner = null;

    public static $flarumBody = null;

    public static $fixSplAutoloadRegister = [];

    public static function boostrap($site) {
        ngx_header_set("Content-Type", "text/html; charset=UTF-8");

        $_SERVER = [];
        $_SERVER["REQUEST_URI"] = ngx_request_uri();
        $_SERVER["QUERY_STRING"] = ngx_request_query_string();
        $_SERVER["REQUEST_METHOD"] = ngx_request_method();
        $_SERVER["REMOTE_ADDR"] = ngx_request_remote_addr();

        $headers = ngx_request_headers();
        foreach ($headers as $k => $v) {
            $_SERVER["HTTP_".strtoupper($k)] = $v;
        }

        $get = ngx_query_args();$_GET = empty($get) ? [] : $get;
        $post = ngx_post_args();$_POST = empty($post) ? [] : $post;
        foreach ($_GET as $k => $v) {
            $_GET[urldecode($k)] = urldecode($v);
            if ($k != urldecode($k)) {
                unset($_GET[$k]);
            }
        }

        if (\function_exists('ngx_request_body')) {
            self::$flarumBody = ngx_request_body();
        }

        if (self::$server == null) {
            self::$server = new Server($site);
        }

        self::$server->listen();

        gc_collect_cycles();
    }

    public static function httpRedirect($uri) {
        if (\function_exists('ngx_redirect')) {
            ngx_redirect((string) $uri);
        }
    }

    public static function httpStatus($status) {
        if (\function_exists("ngx_status")) {
            ngx_status($status);
        }
    }

    public static function httpSetCookie($name, $value, $expires, $maxAge, $path, $domain, $secure, $httpOnly){
        if (\function_exists('ngx_cookie_set')) {

            $arr_cookie = [
                $name => $value,
                'Expires' => $expires,
                'Max-Age' => $maxAge,
                'Path' => $path,
                //'domain' => $obj_cookie->getDomain(),
                //'secure' => $obj_cookie->getSecure(),
                //'httpOnly' => $obj_cookie->getHttpOnly()
            ];
            $str_cookie = [];
            foreach ($arr_cookie as $k => $v) {
                array_push($str_cookie, "$k=$v");
            }
            $str_cookie = implode("; ", $str_cookie);
            ngx_cookie_set($str_cookie);
        }
    }
}