AHdark

AHdark Blog

Senior high school student with a deep passion for coding. Driven by a love for problem-solving, I’m diving into algorithms while honing my skills in TypeScript, Rust, and Golang.
telegram
tg_channel
twitter
github

Nginx Dynamic Reverse Proxy Implementation

The overseas transit links of Source Global CDN often require reverse proxying for multiple domains. The previous solution was to create a separate vhost for each domain. However, in the reality of growing links and increasing servers, this solution has become increasingly difficult to maintain.

As the main operations personnel of Source Global CDN, I naturally took on this task.

Problem#

With the increase in servers used for services and the growth of links, we face the following issues:

  1. When configuration changes occur, content modifications are needed for all servers, which is time-consuming and labor-intensive.
  2. If new services are added, multiple operations need to be performed on all servers, and relevant testing is required.
  3. Management of SSL certificates and vhost conf files is chaotic.

After careful consideration, we roughly came up with the following solutions:

  1. Use wildcard domain resolution to improve cohesion, allowing all servers to automatically cooperate with addition, deletion, and modification operations without separate configuration.
  2. Use a private DNS service to manage links on a single platform without needing modifications on the servers.

The highly cohesive wildcard domain resolution solution significantly reduces the number of vhost files and SSL certificates, while the private DNS avoids the issue of unmodified data within the servers.

Implementation#

Wildcard Domain Resolution#

After consideration, we formulated the following plan:

  1. A SaaS task is responsible for using acme.sh to operate DNS and apply for wildcard SSL certificates.
  2. Store the certificates in a bucket and update the Content-MD5 meta through another SaaS task.
  3. The server's Crontab automatically retrieves MD5 from the bucket every hour and compares it. If there are changes, it updates the local SSL and reloads Nginx.

The acme.sh operation can refer to Using acme.sh to Automatically Configure Certificates for IP/Domain with slight modifications.

Nginx Dynamic Reverse Proxy#

After completing the above simple tasks, we began to consider Nginx's dynamic reverse proxy.

The reason for not considering the use of Lua scripts is that the time cost of reinstalling OpenResty on all servers is too great.

Most people are likely already familiar with Nginx's reverse proxy, which typically presents the following pattern:

In this pattern, the reverse proxy always fetches resources from 127.0.0.1:8080, which is static.

Our requirements are as follows:

  • When $host = gh.sourcegcdn.com, reverse proxy to https://gh.origin.sgcdn
  • When $host = wp.sourcegcdn.com, reverse proxy to https://wp.origin.sgcdn
  • When $host = <subdomain>.sourcegcdn.com, reverse proxy to https://<subdomain>.origin.sgcdn

That is, seek the prefix based on the subdomain of the request header and complete the suffix before performing the reverse proxy.

Our final implementation is as follows:

DNS Resolution#

With this configuration, we only need to set up the CDN to configure the unified node *.sourcegcdn.com, and then add DNS resolution as needed to use it normally.

References#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.