Mapping subdomains to URLs with nginx

后端 未结 3 782
渐次进展
渐次进展 2021-01-01 03:38

I\'m very new to nginx, so forgive me if my explanations are off. I\'ll do my best to explain what I am trying to achieve.

Using WordPress and nginx, I would like us

3条回答
  •  难免孤独
    2021-01-01 04:08

    the following should do it:

    server {
      listen 80; listen 443;
      server_name *.example.com;
    
      if ($host ~ "^(.*)\.example\.com$" ) { set $subdomain $1;}
      rewrite ^ $scheme://example.com/$subdomain/$request_uri permanent;
    }
    

    (as an aside: the regex ^ matches all url's the most efficiently, and the standard nginx variable $request_uri holds the uri including arguments so you don't need the (.*) group in the rewrite)

    additionally add a second serverblock for the domains you don't want redirected:

    server {
      listen 80; listen 443;
      server_name cdn.example.com admin.example.com;
      # do whatever with the requests of the reserved subdomains;
    }
    

提交回复
热议问题