【问题标题】:Rewrite wildcard subdomain to the concrete top-level domain将通配符子域重写为具体的顶级域
【发布时间】:2016-03-27 13:17:00
【问题描述】:

如何配置nginx将<somesubdomain>.mydomain.com重写为mydomain.com/some/url/path/<somesubdomain>/

somesubdomain 是通配符子域。

主要要求是 NOT REDIRECT,<somesubdomain>.mydomain.com 应该是 mydomain.com/some/url/path/<somesubdomain>/ 的掩码。

此外,访问不同于/ 的其他网址(如somesubdomain.mydomain.com/test/)不应显示任何内容。

请注意,我为mydomain.com 配置了/ 的代理,因此/some/url/path/<somesubdomain>/ 应由代理服务器传递和解析:

    location / {
        proxy_pass         http://app_servers;
        proxy_redirect     off;
        proxy_set_header   Host $http_host;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
    }

【问题讨论】:

    标签: url nginx url-rewriting


    【解决方案1】:

    它适用于此配置(由当前server{} 部分添加):

    if ($host ~* (?<subdomain>[a-z0-9]+)\.mydomain\.com) {
        rewrite ^/$ /some/path/$store_subdomain break;
    }
    

    【讨论】:

      【解决方案2】:
      server {
         # server name with regexp
         server_name ~^(?<sub>[^.]+)\.mydomain\.com$;
         # now this server will catch all requests to xxxx.mydomain.com
         # and put "xxxx" to $sub variable
      
         # location _only_ for "/" URI
         # we can do it using "=" sign (means "exactly")
         location = / {
              # finally we want to request different URI from remote server
              proxy_pass http://app_servers/some/url/path/$sub/;
              # proxy_redirect will rewrite Location: header from backend
              # or you can leave proxy_redirect off;
              proxy_redirect http://app_servers/some/url/path/$sub/ http://$sub.mydomain.com/;
              ....
         }
         # next location for all other requests
         location / {
            return 404;
         }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-18
        • 1970-01-01
        • 2014-12-29
        • 2012-08-09
        • 2014-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多