Recently, Nginx officially released the 1.0 version of nginx unit. As a member of Nginx, it is inevitable to experience it first.
unit is a dynamic web application server that uses a mechanism similar to php-fpm, but supports multiple languages such as Python/Go/Perl/Ruby/PHP, and will add support for Java/Javascript later.
Configure the source code
clone source code repository
$ git clone https://github.com/nginx/nginx.git
Enter the directory and execute configure
$ ./configure
Configure the go module
Support for go is configured here, other language configurations are similar, please refer to the official documentation
First install the golang environment, you can download the corresponding binary package installation here . Installed can be skipped.
$ wget https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz
$ tar -xf go1.10.1.linux-amd64.tar.gz -C /usr/local/
$ export PATH=$PATH:/usr/local/go/bin
Create a new folder in the user's directory go
as the working directory of go, and set the environment variable GOPATH
$ mkdir $HOME/go
$ export GOPATH=$HOME/go
Configure unit to support golang, the main work is to introduce the "nginx/unit" package for go, which is used to call in go
$ ./configure go
$ make go-install
Create a go program
$ cd $GOPATH
$ mkdir src/unit-demo
$ vim ./src/unit-demo/main.go
The code in main.go is as follows
package main
import (
"fmt"
"net/http"
"nginx/unit"
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain");
fmt.Fprintf(w, "Method : %s\n", r.Method)
fmt.Fprintf(w, "URL : %s\n", r.URL.Path)
fmt.Fprintf(w, "Host : %s\n", r.Host)
}
func main() {
http.HandleFunc("/", handler)
unit.ListenAndServe("8000", nil)
}
The biggest difference between this and the http server implemented by ordinary go is that unit.ListenAndServe is called instead of http.ListenAndServe. When the compiled program is used as a separate program, unit.ListenAndServe is exactly the same as http.ListenAndServe. When starting through the unit, the parameter of uint.ListenAndServe is ignored, and the real listening port is obtained from the main program of the unit through the environment variable, instead of using the 8000 port in the parameter. You can also use other ports instead of "8000" here.
Compile and install the go program
$ go install unit-demo
Compile and execute unit
$ cd /path/to/unit
$ make
$ ./build/unitd
configure unit
The unit seems to have few configuration items, and it can only be configured through unix socket. Here, it is simple to use curl to configure
Create a new unit.config text file and enter the following configuration
{
"applications": {
"example_go": {
"type": "go",
"executable": "/home/kenan/go/bin/unit-demo"
}
},
"listeners": {
"*:8500": {
"application": "example_go"
}
}
}
In the configuration, listeners represents the listening address and the corresponding handler. Applications corresponds to the information of all handlers. type
Go means the golang program and executable
the executable file path. For HTTP requests on port 8500, the previously generated unit-demo program will handle it
Pass the content of the unit.config file to the unitd program through curl, and the return Reconfiguration done
indicates that the configuration is successful.
$ curl -X PUT -d @unit.config --unix-socket control.unit.sock http://localhost/
{
"success": "Reconfiguration done."
}
Access with curl, you can see the returned results.
$ curl http://localhost:8500/t
Method : GET
URL : /t
Host : localhost:8500
Summarize
Unit is more of a process manager than Nginx. In the past, multiple back-end applications needed to be managed separately, but now unit can be implemented in one program. If a project uses multiple languages such as PHP/Python/Go/Ruby, using unit to manage everything seems to be a good choice.