Introduction
Nginx Unit is a dynamic application server designed to run web applications written in various programming languages. Developed by the creators of the popular Nginx web server, Nginx Unit aims to simplify the deployment and management of modern web applications.
Features of Nginx Unit
- Polyglot Runtime: Nginx Unit supports multiple programming languages including Python, PHP, Perl, Ruby, Go, JavaScript (Node.js), and Java. This makes it ideal for hosting diverse applications on a single server.
- Dynamic Configuration: You can update the configuration of Nginx Unit without restarting it, enabling zero-downtime deployments. This includes adding or removing applications, changing routing rules, and modifying resource limits.
- RESTful API: Nginx Unit is managed via a JSON-based RESTful API, which simplifies automation and integration with other tools.
- Application Isolation: Each application runs in its own process, ensuring that crashes or performance issues in one application do not affect others.
- Security: Nginx Unit offers various security features like chroot environments, dropping privileges, and setting resource limits to enhance security.
- Scalability: Designed to handle high traffic, Nginx Unit can be scaled horizontally to meet increasing demands.
How Nginx Unit is Different from Nginx Server
Nginx Unit and Nginx are both products of the same development team, but they serve different purposes:
- Nginx: Primarily a web server, reverse proxy, load balancer, and HTTP cache. It is highly efficient at serving static content and managing HTTP requests.
- Nginx Unit: An application server that runs dynamic web applications written in various programming languages. It focuses on application management and dynamic configuration rather than static content delivery.
While Nginx excels at handling web traffic and distributing loads, Nginx Unit is designed for running and managing application processes. They can be used together, where Nginx handles the front-end web server tasks and Nginx Unit manages the application back-end.
Nginx Unit Control API
The Nginx Unit Control API endpoint is a local Unix socket by default, used to configure and manage the Nginx Unit server. By default, the Unix socket is located at /var/run/control.unit.sock. You can interact with this socket using tools like curl. For example, to get the current configuration:
curl --unix-socket /var/run/control.unit.sock http://localhost/config
If you prefer to use a TCP socket, you can configure Nginx Unit to listen on a specific IP address and port by editing the configuration file.
{
"listeners": {
"127.0.0.1:8443": {
"pass": "applications/control"
}
}
}
After applying this configuration, you can access the Control API via:
curl http://127.0.0.1:8443/config
Nginx Unit allows you to configure listeners, routes, proxying, response headers, and applications through its RESTful API. Here are detailed examples for each configuration.
1. Listeners
Listeners define the IP address and port where Nginx Unit accepts incoming requests.
Example: Define a Listener
curl -X PUT --data '{
"listeners": {
"*:8083": {
"pass": "routes/first-route"
}
}
}' http://localhost:8443/config
This configuration sets up a listener on port 8083 and passes incoming requests to a route named first-route.
2. Routes
Routes define how requests are processed and directed to applications.
Example: Define a Route
curl -X PUT --data '{
"routes": {
"first-route": [
{
"match": {
"uri": "/example"
},
"action": {
"pass": "applications/example-app"
}
}
]
}
}' http://localhost:8443/config
This route configuration matches requests with the URI /example and directs them to an application named example-app.
3. Proxying
Nginx Unit can act as a reverse proxy, forwarding requests to another server.
Example: Configure Proxying
curl -X PUT --data '{
"routes": {
"proxy-route": [
{
"action": {
"proxy": "http://backend-server:9000"
}
}
]
}
}' http://localhost:8443/config
This configuration proxies all incoming requests to http://backend-server:9000.
4. Setting Response Headers
You can modify response headers using the action configuration.
Example: Set Response Headers
curl -X PUT --data '{
"routes": {
"header-route": [
{
"action": {
"pass": "applications/header-app",
"headers": {
"add": {
"X-Custom-Header": "CustomValue"
}
}
}
}
]
}
}' http://localhost:8443/config
This route adds a custom header X-Custom-Header with the value CustomValue to responses from the application header-app.
5. Applications
Applications are the backend services that handle requests. Nginx Unit supports multiple programming languages.
Example: Define a Node.js Application
- First, create a simple Node.js application.
/mypath/app.js
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, World!\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
- Define the Application Configuration:
curl -X PUT --data '{
"listeners": {
"*:8300": {
"pass": "applications/myapp"
}
},
"applications": {
"myapp": {
"type": "node",
"working_directory": "/mypath",
"executable": "app.js"
}
}
}' http://localhost:8443/config
With this configuration:
- Requests to
http://localhost:8300/will be handled by the Node.js application.
Conclusion
Nginx Unit offers a powerful and flexible way to manage web applications with dynamic configuration capabilities. By using the RESTful API, you can easily set up listeners, routes, proxies, response headers, and applications to create a robust and scalable application server environment.
