Dynamic configuration in NGINX Plus allows you to make changes to your NGINX configuration without needing to reload or restart the server. This feature is particularly useful for environments that require high availability and minimal downtime. In this section, we will cover the following topics:
- Introduction to Dynamic Configuration
- Using the NGINX Plus API
- Dynamic Upstream Configuration
- Practical Examples
- Exercises
- Introduction to Dynamic Configuration
Dynamic configuration enables you to:
- Add or remove servers from upstream groups.
- Change load balancing methods.
- Modify server weights and other parameters.
This is achieved through the NGINX Plus API, which provides a RESTful interface for managing NGINX configuration dynamically.
- Using the NGINX Plus API
The NGINX Plus API is a powerful tool that allows you to interact with your NGINX configuration programmatically. Here are the key endpoints you will use:
- /api/6/http/upstreams: Manage upstream groups.
- /api/6/http/servers: Manage HTTP servers.
- /api/6/stream/upstreams: Manage stream upstream groups.
- /api/6/stream/servers: Manage stream servers.
Example: Adding a Server to an Upstream Group
To add a server to an upstream group, you can use the following curl
command:
curl -X POST -d '{"server": "192.168.1.100:80"}' \ http://localhost:8080/api/6/http/upstreams/my_upstream/servers
Explanation:
-X POST
: Specifies the HTTP method.-d '{"server": "192.168.1.100:80"}'
: The data to be sent in JSON format.http://localhost:8080/api/6/http/upstreams/my_upstream/servers
: The API endpoint.
- Dynamic Upstream Configuration
Dynamic upstream configuration allows you to manage the servers in your upstream groups without reloading NGINX. This is particularly useful for load balancing and high-availability setups.
Example: Removing a Server from an Upstream Group
To remove a server from an upstream group, you can use the following curl
command:
Explanation:
-X DELETE
: Specifies the HTTP method.http://localhost:8080/api/6/http/upstreams/my_upstream/servers/192.168.1.100:80
: The API endpoint.
- Practical Examples
Example 1: Changing Load Balancing Method
To change the load balancing method for an upstream group, you can use the following curl
command:
curl -X PATCH -d '{"method": "least_conn"}' \ http://localhost:8080/api/6/http/upstreams/my_upstream
Example 2: Modifying Server Weight
To modify the weight of a server in an upstream group, you can use the following curl
command:
curl -X PATCH -d '{"weight": 10}' \ http://localhost:8080/api/6/http/upstreams/my_upstream/servers/192.168.1.100:80
- Exercises
Exercise 1: Add a Server to an Upstream Group
Task: Add a server with the IP address 192.168.1.101
and port 8080
to the upstream group my_upstream
.
Solution:
curl -X POST -d '{"server": "192.168.1.101:8080"}' \ http://localhost:8080/api/6/http/upstreams/my_upstream/servers
Exercise 2: Remove a Server from an Upstream Group
Task: Remove the server with the IP address 192.168.1.101
and port 8080
from the upstream group my_upstream
.
Solution:
Exercise 3: Change Load Balancing Method
Task: Change the load balancing method of the upstream group my_upstream
to ip_hash
.
Solution:
Exercise 4: Modify Server Weight
Task: Change the weight of the server 192.168.1.100:80
in the upstream group my_upstream
to 5
.
Solution:
curl -X PATCH -d '{"weight": 5}' \ http://localhost:8080/api/6/http/upstreams/my_upstream/servers/192.168.1.100:80
Conclusion
In this section, we covered the basics of dynamic configuration in NGINX Plus, including how to use the NGINX Plus API to manage upstream groups and servers dynamically. We also provided practical examples and exercises to help you get hands-on experience with dynamic configuration. This knowledge is crucial for maintaining high availability and minimizing downtime in production environments.