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.37k stars 323 forks source link

POST Method isn't allowed #640

Closed lacosteque closed 2 years ago

lacosteque commented 2 years ago

Hi Why doesn't Unit understand post request? For example I'm making a POST request from the manual

sudo curl -X POST -d '{"match: {"uri":"/production/*"}, \
       { "action": {"pass": "applications/wiki-prod"}}'  \
       --unix-socket=/path/to/control.unit.sock \
       http://localhost/config/routes/

But the config doesn't update, but gives an error

{
    "error": "Method isn't allowed."
}

$unitd --version

unit version: 1.26.1
configured as ./configure --libdir=/usr/lib64 --prefix=/usr --state=/var/lib/unit --control=unix:/run/unit/control.sock --pid=/run/unit/unit.pid --log=/var/log/unit/unit.log --tmp=/var/tmp --user=unit --group=unit --openssl --cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection' --tests --modules=/usr/lib64/unit/modules
hongzhidao commented 2 years ago

@lacosteque

I found your JSON object is invalid. According to the docs, it works fine.

Let’s add a route to the prod app: POST always adds to the array end, so there’s no need for an index:

curl -X POST -d '{"match": {"uri": "/production/*"}, \
       "action": {"pass": "applications/wiki-prod"}}'  \
       --unix-socket=/path/to/control.unit.sock        \
       http://localhost/config/routes/

Note it requires the type of entry corresponding to the request path like http://localhost/config/routes/ is array. Of course, if you post a path like http://localhost/config/, it returns the same error.

VBart commented 2 years ago

@lacosteque Could you show the result of sudo curl --unix-socket=/path/to/control.unit.sock http://localhost/config/routes/ command?

Note, that POST in /config/routes/ will work only if the routes object was previously configured as an array.

lacosteque commented 2 years ago

@lacosteque Could you show the result of sudo curl --unix-socket=/path/to/control.unit.sock http://localhost/config/routes/ command?

Note, that POST in /config/routes/ will work only if the routes object was previously configured as an array.

Yes, you are right. POST works, you need the correct configuration of the config.

Then maybe you can advise me on best practices and how to build the right config for my applications?

I have several projects on wordpress. At the moment the config for it is configured as follows...

{
    "settings": {
        "http": {
            "header_read_timeout": 30,
            "body_read_timeout": 30,
            "send_timeout": 30,
            "idle_timeout": 180,
            "max_body_size": 16777216
        }
    },

    "listeners": {
        "127.0.0.1:8080": {
            "pass": "routes/example.com",
            "client_ip": {
                "header": "X-Forwarded-For",
                "recursive": true,
                "source": ["127.0.0.1"]
            }
        }
    },

    "routes": {
        "example.com": [
            {
                "match": {
                    "uri": ["*.php", "*.php/*", "/wp-admin/"]
                },

                "action": {
                    "pass": "applications/example.com/direct"
                }
            },
            {
                "action": {
                    "share": "/var/www/example.com/public/example.com$uri",
                    "fallback": {
                        "pass": "applications/example.com/index"
                    }
                }
            }
        ]
    },

    "applications": {
        "example.com": {
            "type": "php",
            "user": "examplecomru",
            "group": "examplecomru",
            "processes": {
                "max": 30,
                "spare": 1
            },

            "options": {
                "file": "/var/www/example.com/data/config/php.ini"
            },

            "isolation": {
                "namespaces": {
                    "cgroup": true,
                    "credential": true,
                    "mount": true,
                    "network": false,
                    "pid": true,
                    "uname": true
                }
            },

            "environment": {
                "DB_NAME": "",
                "DB_USER": "",
                "DB_PASSWORD": "",
                "DB_HOST": "",
                "URL": ""
            },

            "targets": {
                "direct": {
                    "root": "/var/www/example.com/public/example.com/"
                },

                "index": {
                    "root": "/var/www/example.com/public/example.com",
                    "script": "index.php"
                }
            }
        }
    }
}

For a few like this ...

    "settings": {
        "http": {
            "header_read_timeout": 30,
            "body_read_timeout": 30,
            "send_timeout": 30,
            "idle_timeout": 180,
            "max_body_size": 16777216
        }
    },
    "listeners": {
        "127.0.0.1:8080": {
            "pass": "routes/example.com",
            "client_ip": {
                "header": "X-Forwarded-For",
                "recursive": true,
                "source": ["127.0.0.1"]
            }
        },

        "127.0.0.1:8081": {
            "pass": "routes/example2.com",
            "client_ip": {
                "header": "X-Forwarded-For",
                "recursive": true,
                "source": ["127.0.0.1"]
            }
        }
    },

    "routes": {
        "example.com": [
            {
                "match": {
                    "uri": ["*.php", "*.php/*", "/wp-admin/"]
                },
                "action": {
                    "pass": "applications/example.com/direct"
                }
            },
            {
                "action": {
                    "share": "/var/www/example.com/public/example.com$uri",
                    "fallback": {
                        "pass": "applications/example.com/index"
                    }
                }
            }
        ],

        "example2.com": [
            {
                "match": {
                    "uri": ["*.php", "*.php/*", "/wp-admin/"]
                },
                "action": {
                    "pass": "applications/example2.com/direct"
                }
            },
            {
                "action": {
                    "share": "/var/www/example2.com/public/example2.com$uri",
                    "fallback": {
                        "pass": "applications/example2.com/index"
                    }
                }
            }
        ]
    },
    "applications": {
        "example.com": {
            "type": "php",
            "user": "root",
            "group": "root",
            "processes": {
                "max": 30,
                "spare": 1
            },
            "options": {
                "file": "/var/www/example.com/data/config/php.ini"
            },

            "isolation": {
                "namespaces": {
                    "cgroup": true,
                    "credential": true,
                    "mount": true,
                    "network": false,
                    "pid": true,
                    "uname": true
                }
            },

            "environment": {
                "DB_NAME": "",
                "DB_USER": "",
                "DB_PASSWORD": "",
                "DB_HOST": "",
                "URL": ""
            },

            "targets": {
                "direct": {
                    "root": "/var/www/example.com/public/example.com/"
                },
                "index": {
                    "root": "/var/www/example.com/public/example.com",
                    "script": "index.php"
                }
            }
        },

        "example2.com": {
            "type": "php",
            "user": "root",
            "group": "root",
            "processes": {
                "max": 30,
                "spare": 1
            },
            "options": {
                "file": "/var/www/example2.com/data/config/php.ini"
            },

            "isolation": {
                "namespaces": {
                    "cgroup": true,
                    "credential": true,
                    "mount": true,
                    "network": false,
                    "pid": true,
                    "uname": true
                }
            },

            "environment": {
                "DB_NAME": "",
                "DB_USER": "",
                "DB_PASSWORD": "",
                "DB_HOST": "",
                "URL": ""
            },

            "targets": {
                "direct": {
                    "root": "/var/www/example2.com/public/example2.com/"
                },
                "index": {
                    "root": "/var/www/example2.com/public/example2.com",
                    "script": "index.php"
                }
            }
        }
    }
}

Is there any way to lighten them up through routes? To use 1 listener? If not, how in my case is it possible to add other apps via POST? Thanks

hongzhidao commented 2 years ago

@lacosteque

To use 1 listener?

Yes, Unit can do it. https://unit.nginx.org/configuration/#variables