search
HomeOperation and MaintenanceNginxBuilding a distributed system: using Nginx Proxy Manager to implement service discovery and routing

构建分布式系统:利用Nginx Proxy Manager实现服务发现与路由

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.

  1. 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.
  2. 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.

  1. 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.

  1. 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.

  1. 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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Advantages of NGINX: Speed, Efficiency, and ControlThe Advantages of NGINX: Speed, Efficiency, and ControlMay 12, 2025 am 12:13 AM

The reason why NGINX is popular is its advantages in speed, efficiency and control. 1) Speed: Adopt asynchronous and non-blocking processing, supports high concurrent connections, and has strong static file service capabilities. 2) Efficiency: Low memory usage and powerful load balancing function. 3) Control: Through flexible configuration file management behavior, modular design facilitates expansion.

NGINX vs. Apache: Community, Support, and ResourcesNGINX vs. Apache: Community, Support, and ResourcesMay 11, 2025 am 12:19 AM

The differences between NGINX and Apache in terms of community, support and resources are as follows: 1. Although the NGINX community is small, it is active and professional, and official support provides advanced features and professional services through NGINXPlus. 2.Apache has a huge and active community, and official support is mainly provided through rich documentation and community resources.

NGINX Unit: An Introduction to the Application ServerNGINX Unit: An Introduction to the Application ServerMay 10, 2025 am 12:17 AM

NGINXUnit is an open source application server that supports a variety of programming languages ​​and frameworks, such as Python, PHP, Java, Go, etc. 1. It supports dynamic configuration and can adjust application configuration without restarting the server. 2.NGINXUnit supports multi-language applications, simplifying the management of multi-language environments. 3. With configuration files, you can easily deploy and manage applications, such as running Python and PHP applications. 4. It also supports advanced configurations such as routing and load balancing to help manage and scale applications.

Using NGINX: Optimizing Website Performance and ReliabilityUsing NGINX: Optimizing Website Performance and ReliabilityMay 09, 2025 am 12:19 AM

NGINX can improve website performance and reliability by: 1. Process static content as a web server; 2. forward requests as a reverse proxy server; 3. allocate requests as a load balancer; 4. Reduce backend pressure as a cache server. NGINX can significantly improve website performance through configuration optimizations such as enabling Gzip compression and adjusting connection pooling.

NGINX's Purpose: Serving Web Content and MoreNGINX's Purpose: Serving Web Content and MoreMay 08, 2025 am 12:07 AM

NGINXserveswebcontentandactsasareverseproxy,loadbalancer,andmore.1)ItefficientlyservesstaticcontentlikeHTMLandimages.2)Itfunctionsasareverseproxyandloadbalancer,distributingtrafficacrossservers.3)NGINXenhancesperformancethroughcaching.4)Itofferssecur

NGINX Unit: Streamlining Application DeploymentNGINX Unit: Streamlining Application DeploymentMay 07, 2025 am 12:08 AM

NGINXUnit simplifies application deployment with dynamic configuration and multilingual support. 1) Dynamic configuration can be modified without restarting the server. 2) Supports multiple programming languages, such as Python, PHP, and Java. 3) Adopt asynchronous non-blocking I/O model to improve high concurrency processing performance.

NGINX's Impact: Web Servers and BeyondNGINX's Impact: Web Servers and BeyondMay 06, 2025 am 12:05 AM

NGINX initially solved the C10K problem and has now developed into an all-rounder who handles load balancing, reverse proxying and API gateways. 1) It is well-known for event-driven and non-blocking architectures and is suitable for high concurrency. 2) NGINX can be used as an HTTP and reverse proxy server, supporting IMAP/POP3. 3) Its working principle is based on event-driven and asynchronous I/O models, improving performance. 4) Basic usage includes configuring virtual hosts and load balancing, and advanced usage involves complex load balancing and caching strategies. 5) Common errors include configuration syntax errors and permission issues, and debugging skills include using nginx-t command and stub_status module. 6) Performance optimization suggestions include adjusting worker parameters, using gzip compression and

Nginx Troubleshooting: Diagnosing and Resolving Common ErrorsNginx Troubleshooting: Diagnosing and Resolving Common ErrorsMay 05, 2025 am 12:09 AM

Diagnosis and solutions for common errors of Nginx include: 1. View log files, 2. Adjust configuration files, 3. Optimize performance. By analyzing logs, adjusting timeout settings and optimizing cache and load balancing, errors such as 404, 502, 504 can be effectively resolved to improve website stability and performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools