Change Nginx Upstreamserver via API

Started by vardenx, June 23, 2022, 02:34:35 PM

Previous topic - Next topic
Hi,
I would like to change the upstream server for Nginx via the API.
I'm able to change several attributes with the API, but the one thing I'd like to do doesn't work.

This (for example) works:
curl --location --request POST 'https://10.10.13.1:8443/api/nginx/settings/setupstreamserver/99bfa024-506b-4b1a-8481-bebe2d02fa41'; \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YldGRXdrV3VxYXRoOVIwWmpFcWkwdng ... ZSXRXTTI=' \
--data-raw '{"upstream_server":{"description":"missrv2"}}'


This doesn't:
curl --location --request POST 'https://10.10.13.1:8443/api/nginx/settings/setupstreamserver/99bfa024-506b-4b1a-8481-bebe2d02fa41'; \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YldGRXdrV3VxYXRoOVIwWmpFcWkwdng ... ZSXRXTTI=' \
--data-raw '{"upstream_server":{"no_use":{"down":{"selected":1}}}}


The error message is:
{
    "errorMessage": "/usr/local/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php:159: Error at /usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php:337 - Array to string conversion (errno=8)",
    "errorTrace": "#0 /usr/local/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php(337): OPNsense\\Base\\ApiControllerBase->APIErrorHandler(8, 'Array to string...', '/usr/local/opns...', 337, Array)\n#1 /usr/local/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php(446): OPNsense\\Base\\FieldTypes\\BaseField->__toString()\n#2 /usr/local/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php(218): OPNsense\\Base\\BaseModel->performValidation()\n#3 /usr/local/opnsense/mvc/app/controllers/OPNsense/Base/ApiMutableModelControllerBase.php(454): OPNsense\\Base\\ApiMutableModelControllerBase->validate(Object(OPNsense\\Base\\FieldTypes\\ContainerField), 'upstream_server')\n#4 /usr/local/opnsense/mvc/app/controllers/OPNsense/Nginx/Api/SettingsController.php(155): OPNsense\\Base\\ApiMutableModelControllerBase->setBase('upstream_server', 'upstream_server', '99bfa024-506b-4...')\n#5 [internal function]: OPNsense\\Nginx\\Api\\SettingsController->setupstreamserverAction('99bfa024-506b-4...')\n#6 [internal function]: Phalcon\\Dispatcher\\AbstractDispatcher->callActionMethod(Object(OPNsense\\Nginx\\Api\\SettingsController), 'setupstreamserv...', Array)\n#7 [internal function]: Phalcon\\Dispatcher\\AbstractDispatcher->dispatch()\n#8 /usr/local/opnsense/www/api.php(26): Phalcon\\Mvc\\Application->handle('/api/nginx/sett...')\n#9 {main}",
    "errorTitle": "An API exception occured"
}


I thought maybe I need to deselect the active one first or do the deactivation of the old and the activation of the new one in one step, but these two other solution approaches didn't work either.
I guess it's more of a syntax problem, but I can't see what that could be.

Can someone give me a hint?

LG,
Roland.

Hi!
Quotedata-raw '{"upstream_server":{"no_use":{"down":{"selected":1}}}}
what is "selected" means?
and can you try something like
data-raw '{"upstream_server":{"description":"missrv2","server":"192.168.1.11","port":"4430","priority":"1","max_conns":"","max_fails":"","fail_timeout":"","no_use":"down"}}'
?

That worked.
"no_use":"down" sets it to permanently unreachable and
"no_use":"backup" sets it to backup server.

Thanks a lot!

June 29, 2022, 03:36:29 PM #3 Last Edit: June 29, 2022, 03:39:15 PM by vardenx
Hi,
another question about restarting the nginx service afterwards:

I thought it would be as easy as this:

curl -k -u 'bWF...':'NQV+...' https://10.10.13.1:8443/api/nginx/service/restart


I get this response:

{"response":[]}

and the service did not restart. At least it has the same PID as before, which should not be the case I guess.

Thanks in advance,
Roland.

Hello!

I'm new to OPNSense administration and there's a lot I don't know yet.

How to login to call the API? I get a response:

{"status":401,"message":"Authentication Failed"}