Post

Nginx unit getting started

Unit is a lightweight open-source server, it is a webserver, application server and a proxy. How to use Unit? Let’s getting started.

Install

please read the official document.

https://unit.nginx.org/installation/

Unit with Docker

1.Pull images

1
sudo docker pull nginx/unit

2.Run images

1
sudo docker run -d --mount type=bind,src="$(pwd)/config/",dst=/docker-entrypoint.d/ --mount type=bind,src="$(pwd)/webapp",dst=/www -p 8080:8080 docker.io/nginx/unit:latest

3.Directory permission setting

1
chown -R nobody:nogroup "$(pwd)"

4.Put config and webapp files to “$(pwd)/config/” and “$(pwd)/webapp”

config example, add a config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    "listeners": {
        "*:8080": {
            "pass": "routes"
        }
    },

    "routes": [
        {
            "action": {
                "share": "/www/"
            }
        }
    ]
}

5.Put config to Unit

1
2
3
4
5
sudo docker exec -it contaniersID curl -X PUT --data-binary @/docker-entrypoint.d/config.json  --unix-socket /var/run/control.unit.sock http://localhost/config

#Delete config

sudo docker exec -it 4e6cded816dd curl -X DELETE --data-binary @/docker-entrypoint.d/config.json --unix-socket /var/run/control.unit.sock  http://localhost/config

Unit with Linux (Centos, Ubuntu, Debian, etc.)

  1. setting config

config example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
    "listeners": {
        "*:8080": {
            "pass": "routes"
        }
    },

    "routes": [
        {
            "action": {
                "share": "/home/www$uri"
            }
        }
    ]
}
  1. Put config to Unit
1
2
3
4
sudo curl -X PUT --data-binary @/$(pwd)/config.json  --unix-socket /var/run/control.unit.sock http://localhost/config

#Delete config
sudo curl -X PUT --data-binary @/$(pwd)/config.json  --unix-socket /var/run/control.unit.sock http://localhost/config
This post is licensed under CC BY 4.0 by the author.