


Building a distributed system: using Nginx Proxy Manager to implement service discovery and routing
Building a distributed system: using Nginx Proxy Manager to implement service discovery and routing
Overview:
In modern distributed systems, service discovery and routing are Very important feature. Service discovery allows the system to automatically discover and register available service instances, while routing ensures that requests are correctly forwarded to the appropriate service instance. In this article, we will introduce how to leverage Nginx Proxy Manager to build a simple yet powerful service discovery and routing solution, and provide specific code examples.
- Understand Nginx Proxy Manager
Nginx Proxy Manager is an Nginx-based proxy server manager that provides an easy-to-use web interface to configure and manage reverse proxy servers. It supports HTTP, HTTPS, TCP and UDP proxies, and can implement functions such as request load balancing and SSL termination. - Install and configure Nginx Proxy Manager
First, we need to install Nginx Proxy Manager. It can be installed through the following command:
npm install -g nginx-proxy-manager
After the installation is complete, you can use the following command to start Nginx Proxy Manager:
npm start
After starting, you can access http:/ through the browser /localhost:81 to open the web interface of Nginx Proxy Manager. When accessing for the first time, you need to set an administrator username and password.
- Configuring Service Discovery
In the web interface of Nginx Proxy Manager, you can configure service discovery by adding "Upstreams". Each Upstream represents a service, which contains multiple instances (nodes). In each Upstream, you can specify the IP address and port number of the instance.
The following is an example Upstream configuration:
Name: my_service Servers: - Name: server1 Address: 192.168.0.1:8000 - Name: server2 Address: 192.168.0.2:8000
In this configuration, we created an Upstream named my_service, which contains two instances, namely 192.168.0.1 :8000 and 192.168.0.2:8000.
- Configure routing
In the web interface of Nginx Proxy Manager, you can configure routing by adding "Proxy Hosts". Each Proxy Host represents a routing rule, which defines the source and destination of requests.
The following is a sample Proxy Host configuration:
Domain Name: mydomain.com Path: /myroute Upstream: my_service
In this configuration, we create a route that forwards all requests from mydomain.com/myroute to my_service Upstream rule.
- Using sample code
To demonstrate the use of Nginx Proxy Manager, the following is a simple Node.js sample code to start an HTTP server and register it as an instance of the service Go to Nginx Proxy Manager:
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello, world!'); }); app.listen(8000, () => { console.log('Server is running on http://localhost:8000'); // TODO: Register the server with Nginx Proxy Manager });
In this sample code, we start an HTTP server listening on port 8000. In order to register the service with Nginx Proxy Manager, you need to add the corresponding registration code in the callback function that starts the server.
You can use the API provided by Nginx Proxy Manager to register and deregister service instances. The following is a sample code for registering a service instance with Nginx Proxy Manager:
const axios = require('axios'); const registerInstance = async (name, address) => { try { await axios.post('http://localhost:81/api/proxy/host', { name, target: address, }); console.log(`Instance ${name} registered successfully`); } catch (error) { console.error(`Failed to register instance ${name}`, error); } }; // Register the server instance with Nginx Proxy Manager registerInstance('server1', 'http://192.168.0.1:8000');
In this sample code, we use the axios library to send HTTP requests. Register a service instance by calling the registerInstance
function, passing the instance name and address to the Nginx Proxy Manager's API. You need to ensure that the address requested by the API is consistent with the actual address of Nginx Proxy Manager.
By running this sample code on multiple servers, you can register them as instances of the service and use the Nginx Proxy Manager to implement service discovery and routing.
Summary:
By using Nginx Proxy Manager to build the service discovery and routing functions of the distributed system, the configuration and management of the system can be simplified and the reliability and scalability of the system can be improved. This article introduces the installation and configuration method of Nginx Proxy Manager, and provides specific code examples to demonstrate how to register service instances and configure routing rules. Readers can further adjust and extend these code examples to meet the needs of their own distributed systems.
The above is the detailed content of Building a distributed system: using Nginx Proxy Manager to implement service discovery and routing. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]
