域名切换方案

2023/2/17

# 域名切换方案

以主域名为: ith.thsrm.com举例

# 多域名

多应用确实有需要通过多域名区分访问的需要,原因见下:

1.CDN 缓存更方便。

2.突破浏览器并发限制。

3.节约 cookie 带宽。

4.节约主域名的连接数,优化页面响应速度。

5.防止不必要的安全问题。

6.使用多个域名,则可以将网站的内容分类清晰地展示给用户,让用户更容易找到自己需要的内容。

7.如果只使用一个域名,那么这个网站的内容就会很集中,这样一来,网站的内容就会显得很杂乱无章。

# srm平台域名

http://admin.ith.thsrm.com/ (管理员端)

http://purchase.ith.thsrm.com/ (采购平台端)

http://sso.ith.thsrm.com/ (登录端)

http://preview.ith.thsrm.com/ (插件-在线预览)

http://datav.ith.thsrm.com/ (插件-数据大屏)

http://bpm.ith.thsrm.com/ (审批流平台)

# 电商平台域名

http://admin.ith.thsrm.com/ (管理员端)

http://channel.ith.thsrm.com/ (销售平台端)

http://tmall.ith.thsrm.com/ (前台商城端)

http://sso.ith.thsrm.com/ (登录端)

http://preview.ith.thsrm.com/ (插件-在线预览)

http://datav.ith.thsrm.com/ (插件-数据大屏)

http://bpm.ith.thsrm.com/ (审批流平台)

# 单域名

# srm平台域名

以域名为: srm.ith.thsrm.com举例

http://srm.ith.thsrm.com/ (默认采购门户)

http://srm.ith.thsrm.com/purchaseview (采购平台端)

http://srm.ith.thsrm.com/adminView (管理员端)

http://srm.ith.thsrm.com/ssoview (登录端)

http://srm.ith.thsrm.com/preview(插件-在线预览)

http://srm.ith.thsrm.com/datav (插件-数据大屏)

http://srm.ith.thsrm.com/bpmview (审批流平台)

# 电商平台域名

以域名为: mall.ith.thsrm.com举例

http://mall.ith.thsrm.com/ (默认前台商城)

http://mall.ith.thsrm.com/channelview (销售平台端)

http://mall.ith.thsrm.com/mallview (前台商城端)

http://mall.ith.thsrm.com/adminView (管理员端)

http://mall.ith.thsrm.com/ssoview (登录端)

http://mall.ith.thsrm.com/datav (插件-数据大屏)

http://mall.ith.thsrm.com/bpmview (审批流平台)

# 注意事项

单域名切换是 ng需要配置以上资源的代理路径,在线预览不支持单域名部署

# 单域名代码配置

# 定义环境变量

该变量用于当前前端资源跳转其他前端资源的标识

image-20230217143331325

# 修改config/index.js

该文件配置了当前前端跳转其他前端资源的路由控制

image-20230217120644259

   // eslint-disable-next-line no-empty
    // 单域名切换配置
    if (process.env.VUE_DOMAIN === 'single_domain') {
      return window.location.origin + '/xxx'
    }

    xxx表示跳转服务的路径标识,即跳转服务src/router/index.js文件下 VueRouter方法的base (见下图)  

image-20230217120926908

# 修改src/utils/auth.js

与上述同理

image-20230217121127771

export function redirectRegisterUrl() {
  // 单域名切换配置
  if (process.env.VUE_DOMAIN === 'single_domain') {
    window.location.href = $config.ssoPath() + '/ssoview/register?type=3&&redirectUrl=' + encodeURIComponent(window.location.origin + '/mallview' + Arg.url(_.omit(Arg.query(), 'tkn')))
  } else {
    window.location.href = $config.ssoPath() + '/ssoview/register?type=3&&redirectUrl=' + encodeURIComponent(window.location.origin + Arg.url(_.omit(Arg.query(), 'tkn')))
  }
}

# 单域名ng配置

ng需要代理各个前端资源的路径,配置见下图

image-20230217121347670

#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;

    #gzip  on;

    server {
        listen       80;
        server_name  mall.b2b.ciip.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {
            root /usr/local/workespace/jy-b2b/ith-tmall/dist;
            try_files $uri $uri/ /index.html
            index  index.html index.htm;
        }
        location ~^/adminview {
            alias /usr/local/workespace/jy-b2b/ith-admin/dist;
            try_files $uri $uri/ /index.html
            index  index.html index.htm;
        }
        location /ithadmin {
            root /usr/local/workespace/jy-b2b/ith-admin/dist;
        }

        location ~^/channelview {
            alias /usr/local/workespace/jy-b2b/ith-channel/dist;
            try_files $uri $uri/ /index.html
            index  index.html index.htm;
        }
        location /ithchannel {
            root /usr/local/workespace/jy-b2b/ith-channel/dist;
        }

        location ~^/ssoview {
            alias /usr/local/workespace/jy-b2b/ith-sso/dist;
            try_files $uri $uri/ /index.html
            index  index.html index.htm;
        }
        location /ithsso {
            root /usr/local/workespace/jy-b2b/ith-sso/dist;
        }

        location ~^/mallview {
            alias /usr/local/workespace/jy-b2b/ith-tmall/dist;
            try_files $uri $uri/ /index.html
            index  index.html index.htm;
        }
        location /tmall {
            root /usr/local/workespace/jy-b2b/ith-tmall/dist;
        }
#         
#         location ~^/bpm {
#             alias D:\my-job\project\workspace-sx\ith-sso\dist;
#             try_files $uri $uri/ /index.html
#             index  index.html index.htm;
#         }
#         location /bpmstatic {
#             root D:\my-job\project\workspace-sx\ith-bpm\dist;
#         }

        location @router {
            rewrite ^.*$ /index.html last;
        }



        location /api/ {
            proxy_pass   http://112.123.8.160:31068/api/;
        }
        location /jmreport/ {
            proxy_pass   http://112.123.8.160:31068/jmreport/;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

至此以上完成单域名的切换配置!