CMD_UNIT: Nginx Unit for other handlers

This feature is part of the "DirectAdmin Pro Pack".

Nginx Unitopen in new window allows for mid-level processing, after the webserver accepts the requests, but can redirect the request with immense control, before it actually hits the disk.

How to enable Nginx Unit

Build Nginx Unit by running the following commands:

cd /usr/local/directadmin/custombuild
./build set unit yes
./build unit
1
2
3

Then, at the User level, you should be able to access Advanced Features -> Nginx unit.

FILES

/usr/local/directadmin/data/users/USERNAME/domains/DOMAIN.COM.unit/APP.json
/usr/local/directadmin/data/users/USERNAME/domains/DOMAIN.COM.routes/domain|SUB.domain
1
2

These files are used only for local storage, and to re-fill the live unit daemon.
The "live" data is stored/managed in the unit deamon.

TEMPLATES

/usr/local/directadmin/data/templates/unit_apps.json
/usr/local/directadmin/data/templates/unit_routes.json
1
2

These 2 templates are just pre-fill values for Users to pick from.
Manual editing of raw json is still allowed in the GUI.

CMD_UNIT Requests

LIST

Listing all unit apps and routes:

CMD_UNIT?domain=domain.com&json=yes
1

JSON: returns json, with "applications" array.
Each app index is the app name, eg "test", and the json data within that app is the data from the domains/domain.com.unit/$NAME.json.

Note: Numbers are currently shown as string, eg: "timeout": "10"
which is set to be fixed such that the container supports actual numbers.

A "routes" array[] list is also returned. Will be either: domain.com
and/or:
sub.domain.com

where "subdomain" values are allowed routes specific to that subdomain. Routes applied to a domain are not applied to that subdomain.

APPLICATIONS

CREATE APP

Creating an nginx unit app Method: POST CMD_UNIT domain=domain.com action=create name=appname #saved as "domain.com_appname" in unit json={ ... your app ... }

MODIFY

Modify an existing nginx unit app Method: POST domain=domain.com action=modify name=appname json={ ... your app ... }

DELETE

Method: POST CMD_UNIT json=yes domain=domain.com action=select select0=app1 (select1=app2)

DirectAdmin does not check if the app json file currently exists, in case you're attempting to purge a left-over app from curl, which could be out of sync with the files.

ROUTES

LIST ROUTES

Listing all unit apps and routes:

CMD_UNIT?domain=domain.com&json=yes
1
ROUTE CREATION STEPS

'GET' the items available to create with: CMD_UNIT?domain=es60-64.com&action=route_create&json=yes
CMD_UNIT?domain=es60-64.com&action=route_create&subdomain=sub&json=yes
will return; da_settings["applications"] as a list of all currently available unit apps.
da_settings["actions"] as a list of possible action (pass,proxy,return,share), with a brief description as each value.
route[] the json route currently set for this domain or domain+subdomain. If nothing yet, it will be [].

CREATE ROUTE

CMD_UNIT method: POST action=set_route domain=domain.com (subdomain=sub :optional is saving for a subdomain) routes=[ the json list of matches/actions for this route ]

The "routes" data must be assembled json, as defined here: https://unit.nginx.org/configuration/#routesopen in new window

e.g.,

routes=
[
	{
		"match": 
		{
			"uri": "/v2_site/*"
		},
		"action": 
		{
			"pass": "applications/test.com_php"
		}
	},
	{
		"match": 
		{
			"uri": "/directadmin.com"
		},
		"action": 
		{
			"return": 301,
			"location": "https://directadmin.com"
		}
	},
	{
		"action": 
		{
			"proxy": "http://1.2.3.4:9000"
		}
	}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

The DirectAdmin back-end recursively parses and validates all aspects of the json to prevent cross-user/domain app calls or shared paths not belonging to this User.

DELETE ROUTES

CMD_UNIT method: POST json=yes action=select_routes domain=domain.com (even if deleting a sub.domain route, use "domain.com" here) select0=domain.com (select1=sub.domain.com)

DA will attempt to clear the route from the unit socket first.
If it fails, the local json file will not be removed.
If it failed due to the route not existing, the local json file WILL be removed.


TODO packages/reseller/user.conf switches directadmin.conf option to disable unit modules, eg: unit_modules=perl:php:etc.. Access Logs for unit.

SKINS

files_users.conf:

CMD_UNIT_CREATE=user/app/app_create.html
CMD_ROUTE_EDIT=user/app/route_edit.html
CMD_UNIT=user/app/apps.html
1
2
3

also file for viewing all apps, and editing a given app with auto-fill.
(Make use of HTM insertion to re-use the same form html file for create and edit)

However, we recommend using dynamic json, as Evolution does. The above is only a basic raw textarea json method to control it and does not include the templates like Evo does.

Last Updated: 11/4/2021, 11:57:09 AM