saas system tenant personalized domain name && tenant binding their own domain name solution

The actual demand is similar to the custom page of github
1. Personalized Domain Name
The implementation principle of github is to use pan-domain name resolution for user personalized domain names. This is relatively simple and can be solved by most domain name providers.
The specific operation does not need to be repeated
The configuration using nginx is relatively simple
The openresty configuration is as follows:
server {
listen 8080default;
index index.html index.htm index.php;
root html;
location /{
root html;
}
location /app {
root html;
}
location /default{
root html;
}
}
server {
listen 80;
server_name XXXXXXXXX;// Perform pan-domain name resolution configuration here
index index.html index.htm;
root html2;
location /{
set $subdomain default;
if( $http_host ~*"^(.*)\.XXXXXXXXX\.com"){
set $subdomain $1;// Get a tenant's personalized domain name
}
rewrite ^/(.*)$ /$subdomain/$1 break;
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /app {
root html;
}
location /default{
root html;
}
}
2. Bind the tenant's own domain name
The implementation principle of github: The tenant's own domain name uses cname to bind the personalized domain name of github
Locating the site requested by the user through cname
My implementation is to use openresty combined with redis (here should be combined with its own scene, generally saas system still recommends using an environment, middleware processing will be better)
The principle is as follows:
a. The tenant configures the cname to resolve to its own personalized domain name (usually configured in the domain name buyer's system)
Remarks: You can also directly use a record for ip configuration here
b. Tenants need to apply for the domain name they need to bind on the saas platform
c. The saas operation platform conducts the audit (of course, it is generally passed), and stores the comparison management of the tenant's domain name and the personalized domain name in redis
d. Tenants use their own domain names to log in to the saas system
e. The saas system server (openresty) searches for the tenant's personalized domain name in redis in combination with the requested host
f. The server performs reverse proxying to the tenant's personalized domain name host
The openresty configuration is as follows:
server {
listen 80 default_server;
index index.html index.htm index.php;
resolver 127.0.0.1;// conduct dns My test for parsing is to use everyone's own dns server
root html;
location /{
set $subdomain default;
access_by_lua '
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000)
local ok, err = red:connect("XXXXXXXXX", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local host = ngx.var.host;
local res, err = red:get(host)
ngx.var.subdomain = res
';
proxy_pass http://$subdomain/$url;
proxy_set_header Host $subdomain;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /usr/local/openresty/nginx/logs/openresty.access.log access;
error_log /usr/local/openresty/nginx/logs/openresty.error.log;
}
3. Summary
Reference image:
       
 
 
In fact, the principle is relatively simple, but the combination of openresty will simplify a lot of our development
The above are some of my own practices. In fact, the connection of redis above is best to use the connection pool, and for the request memory cache processing
openresty reference documentation:

Tags: Nginx

Posted by joebWI on Sat, 14 May 2022 18:10:17 +0300