NGINX Unit

App Samples§

Note

These steps assume Unit was already installed with the language module and/or package for each app.

Go§

Let’s configure the following basic app, saved as /www/app.go:

package main

import (
    "io";
    "net/http";
    "nginx/unit"
)

func main() {
    http.HandleFunc("/",func (w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "Hello, Unit!")
    })
    unit.ListenAndServe(":8080", nil)
}

Compile it using the source code from the Go language package you have installed or built earlier:

$ cp -r <package installation path>/src/* $GOPATH/src/
$ go build -o /www/app /www/app.go

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{         \
  "listeners": {                       \
      "*:8080": {                      \
          "pass": "applications/go_app"\
      }                                \
  },                                   \
  "applications": {                    \
      "go_app": {                      \
          "type": "external",          \
          "working_directory": "/www/",\
          "executable": "/www/app"     \
      }                                \
  }                                    \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.

Node.js§

Let’s configure the following basic app, saved as /www/app.js:

#!/usr/bin/env node

require("unit-http").createServer(function (req, res) {
    res.writeHead(200, {"Content-Type": "text/plain"});
    res.end("Hello, Unit!")
}).listen()

Make it executable and link the Node.js language package you’ve installed earlier:

$ cd /www
$ chmod +x app.js
$ npm link unit-http

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{           \
  "listeners": {                         \
      "*:8080": {                        \
          "pass": "applications/node_app"\
      }                                  \
  },                                     \
  "applications": {                      \
      "node_app": {                      \
          "type": "external",            \
          "working_directory": "/www/",  \
          "executable": "app.js"         \
      }                                  \
  }                                      \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.

Java§

Let’s configure the following basic app, saved as /www/index.jsp:

<%@ page language="java" contentType="text/plain" %>
<%= "Hello, Unit!" %>

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{           \
  "listeners": {                         \
      "*:8080": {                        \
          "pass": "applications/java_app"\
      }                                  \
  },                                     \
  "applications": {                      \
      "java_app": {                      \
          "type": "java",                \
          "webapp": "/www/"              \
      }                                  \
  }                                      \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.

Perl§

Let’s configure the following basic app, saved as /www/app.psgi:

my $app = sub {
    return [
        "200",
        [ "Content-Type" => "text/plain" ],
        [ "Hello, Unit!" ],
    ];
};

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{           \
  "listeners": {                         \
      "*:8080": {                        \
          "pass": "applications/perl_app"\
      }                                  \
  },                                     \
  "applications": {                      \
      "perl_app": {                      \
          "type": "perl",                \
          "working_directory": "/www/",  \
          "script": "/www/app.psgi"      \
      }                                  \
  }                                      \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.

PHP§

Let’s configure the following basic app, saved as /www/index.php:

<?php echo "Hello, Unit!"; ?>

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{          \
  "listeners": {                        \
      "*:8080": {                       \
          "pass": "applications/php_app"\
      }                                 \
  },                                    \
  "applications": {                     \
      "php_app": {                      \
          "type": "php",                \
          "root": "/www/"               \
      }                                 \
  }                                     \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.

Python§

Let’s configure the following basic app, saved as /www/wsgi.py:

def application(environ, start_response):
    start_response("200 OK", [("Content-Type", "text/plain")])
    return (b"Hello, Unit!")

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{             \
  "listeners": {                           \
      "*:8080": {                          \
          "pass": "applications/python_app"\
      }                                    \
  },                                       \
  "applications": {                        \
      "python_app": {                      \
          "type": "python",                \
          "path": "/www/",                 \
          "module": "wsgi"                 \
      }                                    \
  }                                        \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.

Ruby§

Let’s configure the following basic app, saved as /www/config.ru:

app = Proc.new do |env|
    ["200", {
        "Content-Type" => "text/plain",
    }, ["Hello, Unit!"]]
end

run app

Upload the app config to Unit and test it:

# curl -X PUT --data-binary '{           \
  "listeners": {                         \
      "*:8080": {                        \
          "pass": "applications/ruby_app"\
      }                                  \
  },                                     \
  "applications": {                      \
      "ruby_app": {                      \
          "type": "ruby",                \
          "working_directory": "/www/",  \
          "script": "config.ru"          \
      }                                  \
  }                                      \
  }' --unix-socket /path/to/control.unit.sock http://localhost/config/

$ curl localhost:8080

    Hello, Unit!

See the Dockerfile with this app here.