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
584 stars 56 forks source link

Require in init_worker_by_php #61

Open joanhey opened 4 years ago

joanhey commented 4 years ago

Hi, I want to require(include) a file in the init_worker_by_php, but later I can't use the functions in the file.

Example

init_worker_by_php '
        require "app.php";
    ';

    server {
        listen       8080 default_server reuseport;

        root /;
        index  index.html;

        location = /db {
            add_header Content-Type application/json;
            content_by_php '
                db(); // Function in app.php
            ';
        }

And return this error: Fatal error: Uncaught Error: Call to undefined function db() in ngx_php eval ...

joanhey commented 4 years ago

The problem was that in app.php exist ' , changed ' to ". And is working now.

But will be good if send an error. The courious think is that when include a file in content_by_php there are no problems if the file have '.

joanhey commented 4 years ago

I like more { than '. But I understand that is not tecnically posible in php. But perhaps another tag will be better.

joanhey commented 4 years ago

A question why work: require "app.php"; and not; require app.php;

rryqszq4 commented 4 years ago

The problem was that in app.php exist ' , changed ' to ". And is working now.

But will be good if send an error. The courious think is that when include a file in content_by_php there are no problems if the file have '.

I think init_worker_by_php maybe have bugs in the implementation. Internally use zend api zend_eval_stringl_ex, it is best to give a simple and complete example.

joanhey commented 4 years ago

https://github.com/TechEmpower/FrameworkBenchmarks/pull/5104 it's working. The app-async.php is for include the Mysql async pool. It's empty for you to add the code.

Now the php include the PDO connection in the init_worker_by_php so don't need to reconnect every time.

The only problem was the ' in the app.php.

joanhey commented 4 years ago

WIll be good if init_worker_by_php can include files with ' like do content_by_php. So we can include any php code.

joanhey commented 4 years ago

Added project to openhub https://www.openhub.net/p/ngx_php7 You can sign and claim as manager of the project.

joanhey commented 4 years ago

Now with the require app.php: Fortunes: 157.266 req/s

rryqszq4 commented 4 years ago

Now with the require app.php: Fortunes: 157.266 req/s

It should look like, abc code optimization improves performance. https://github.com/TechEmpower/FrameworkBenchmarks/pull/5104

rryqszq4 commented 4 years ago

Added project to openhub https://www.openhub.net/p/ngx_php7 You can sign and claim as manager of the project.

Done~

joanhey commented 4 years ago

Like ngx_php don't use OpCache, any include have a big impact.

It's like in your bench: image

A simple Hello world if you include the file versus inline (or included in the init_worker), almost half the req/s.

For best performance, always include the app in the init_worker. But we need to use ' in the code, for include any php.

joanhey commented 4 years ago

Also it's a better way to code all your app in app.php, than in the nginx config files.

rryqszq4 commented 4 years ago

Like ngx_php don't use OpCache, any include have a big impact.

It's like in your bench: image

A simple Hello world if you include the file versus inline (or included in the init_worker), almost half the req/s.

For best performance, always include the app in the init_worker. But we need to use ' in the code, for include any php.

The analysis is very clear, cool~

joanhey commented 4 years ago

Also fixed the slow include now that use OPCache #72 .

Perhaps will be good to add part of that info in the README.

rryqszq4 commented 4 years ago

@joanhey Fixed by https://github.com/rryqszq4/ngx_php7/pull/78, Add directive init_worker_by_php_block