用户自定义二级域名实现

场景一:

  1. 访问abc.realibox.com实际显示pandll.com站点内容,但地址栏仍然显示abc.pandll.com
  2. nginx检测abc未配置时,重定向到pandll.com,地址栏也显示pandll.com

场景二:

  1. 访问abc.pandll.com实际显示pandll.com/u/abc页面内容,地址栏仍然显示abc.pandll.com
  2. web检测abc未配置(即/u/abc不存在)时,跳转至pandll.com,地址栏也显示pandll.com
  1. 利用泛域名解析,将*.pandll.com解析pandll.com站点相同IP地址

  2. Nginx配置如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    server {
    listen 80;
    server_name pandll.com;

    location / {
    try_files $uri /app.html;
    }
    }

    # 场景一
    server {
    listen 80;
    server_name *.pandll.com;

    location / {
    proxy_pass http://pandll.com;
    }
    }

    # 场景二
    server {
    listen 80;
    server_name *.pandll.com;

    location / {
    if ($http_host ~* "^(.*?)\.pandll\.com$") {
    proxy_pass http://pandll.com/u/$1;
    }
    proxy_pass http://pandll.com;
    }
    }

参考资料:
nginx实现二级域名转发