代码之家  ›  专栏  ›  技术社区  ›  Frozenskys

复杂nginx子域重写规则

  •  6
  • Frozenskys  · 技术社区  · 14 年前

    我目前在nginx.conf中有以下(hacky)重写规则,允许动态子域重新定向到一个django实例。

    set $subdomain "";
    set $subdomain_root "";
    set $doit "";
    if ($host ~* "^(.+)\.domain\.com$") {
        set $subdomain $1;
        set $subdomain_root "/profile/$subdomain";
        set $doit TR;
    }
    if (!-f $request_filename) {
        set $doit "${doit}UE";
    }
    if ($doit = TRUE) {
        rewrite ^(.*)$ $subdomain_root$1;
        break;
    }
    

    我确信有一种更有效的方法可以做到这一点,但我需要更改此规则,以便任何请求 *.domain.com/media/* *.domain.com/downloads/* domain.com/media/* domain.com/downloads/* .

    3 回复  |  直到 7 年前
        1
  •  9
  •   blueyed    12 年前

    http://nginx.org/en/docs/http/server_names.html#regex_names

    server {
      listen 80;
      listen 443;
      server_name ~^(?<subdomain>.+)\.domain\.com$
      location / {
        rewrite ^ /profile/$subdomain$request_uri;
      }
    }
    
        2
  •  8
  •   Frozenskys    14 年前

    set $subdomain "";
    set $subdomain_root "";
    if ($host ~* "^(.+)\.domain\.com$") {
        set $subdomain $1;
        set $subdomain_root "/profile/$subdomain";
        rewrite ^(.*)$ $subdomain_root$1;
        break;
    }