nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.41k stars 331 forks source link

add support for variables in match block #909

Closed yosefy closed 1 year ago

yosefy commented 1 year ago

why? if user has convention, we may declare block only once "match": { "host": "test..unit.com" } ---> "match": { "host": "$host" }

and then we can use it in application and share paths:

use case: example multiple wordpress installations in small config when wordpress hostname = directory it is in:

    "routes": [
        {
            "match": {
                "host": "$host",
                "uri": [
                    "*.php",
                    "*.php/*",
                    "/wp-admin/"
                ]
            },
            "action": {
                "pass": "applications/$host/direct"
            }
        },
        {
            "match": {
                "host": "$host""
            },
            "action": {
                "share": "/sites/$host/public$uri",
                "fallback": {
                    "pass": "applications/$host/index"
                }
            }
        }
    ],
    "applications": {
        "test.unit.com": {
            "type": "php",
            "targets": {
                "direct": {
                    "root": "/sites/$host/public/"
                },
                "index": {
                    "root": "/sites/$host/public/",
                    "script": "index.php"
                }
            }
        },
lcrilly commented 1 year ago

Matching "host": "$host" is redundant because it is always true. This configuration should work as you expect without the "host" match.

yosefy commented 1 year ago

yes sorry that is true , so another thing:

if we could teplate vars in application, then we wouldnt need to add multiple application block (having naming convention)

for example like this:

    "routes": [
        {
            "match": {
                "uri": [
                    "*.php",
                    "*.php/*",
                    "/wp-admin/"
                ]
            },
            "action": {
                "pass": "applications/wordpress/direct"
            }
        },
        {
            "action": {
                "share": "/sites/wordpress/public$uri",
                "fallback": {
                    "pass": "applications/wordpress/index"
                }
            }
        }
    ],
    "applications": {
        "wordpress": {
            "stderr": "${host}.log",
            "user": "$host",
            "type": "php",
            "targets": {
                "direct": {
                    "root": "/sites/$host/public/"
                },
                "index": {
                    "root": "/sites/$host/public/",
                    "script": "index.php"
                }
            }
        },
lcrilly commented 1 year ago

I can see the convenience of this approach but it would come at a cost.

Unit currently pre-creates a prototype process for each application with the configuration data for the app. It must also assign a user to own the process. The prototype is then used to fork new application processes as necessary. To do all of this dynamically, based on the variables of the current client request would have a noticeable impact on complexity and application performance.

By using JSON for Unit's configuration format, it is expected that such templating happens elsewhere to produce a configuration that is efficient at runtime.

yosefy commented 1 year ago

thanks it is clear