Coder Social home page Coder Social logo

multitenancydomain's Introduction

MultiTenancyDomain

多租户系统中,openresty反向代理中的域名处理

Openresty 服务器

192.168.0.225

Web应用服务器

192.168.0.203

web应用站点

192.168.0.203:8001 www.abc.com

192.168.0.203:8002 www.a.com

192.168.0.203:8003 www.b.com

192.168.0.203:8004 www.c.com

需求

主站 abc.com

二级域名给租户 a.abc.com 302 a.com 租户域名
a.com

b.com

c.com

Hosts文件

192.168.0.225 www.abc.com
192.168.0.225 a.abc.com
192.168.0.225 b.abc.com
192.168.0.225 c.abc.com
192.168.0.225 www.a.com
192.168.0.225 www.b.com
192.168.0.225 www.c.com

192.168.0.225 abc.com
192.168.0.225 a.abc.com
192.168.0.225 b.abc.com
192.168.0.225 c.abc.com
192.168.0.225 a.com
192.168.0.225 b.com
192.168.0.225 c.com

nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    upstream abc{
        server 192.168.0.203:8001;
    }
    upstream a{
        server 192.168.0.203:8002;
    }
    upstream b{
        server 192.168.0.203:8003;
    }
    upstream c{
        server 192.168.0.203:8004;
    }
    #gzip  on;
    server {
        listen       80;
        server_name  ~^(?<subdomain>.+)\.abc\.com$;
        #charset koi8-r;
        access_log  logs/abc.access.log  main;
        location / {
            rewrite_by_lua '
                if ngx.var.subdomain ~= nil and ngx.var.subdomain ~= "www" and ngx.var.subdomain ~= "" then
                    return ngx.redirect("http://"..ngx.var.subdomain..".com",302)
                end
            ';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://abc;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  *.a.com a.com;
        #charset koi8-r;
        access_log  logs/a.access.log  main;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://a;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  *.b.com b.com;
        #charset koi8-r;
        access_log  logs/b.access.log  main;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://b;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       80;
        server_name  *.c.com c.com;
        #charset koi8-r;
        access_log  logs/c.access.log  main;
        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://c;
        }
        #error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

multitenancydomain's People

Contributors

xewn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

multitenancydomain's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.