nginx子域重写

时间:2010-05-07 01:03:16

标签: url-rewriting nginx

又一个nginx重写规则问题:

如何从http://www.*.domain.comhttp://*.domain.com重写?

2 个答案:

答案 0 :(得分:7)

if ($host ~* www\.(.*)) {
  set $host_without_www $1;
  rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}

从服务器故障中回答: https://serverfault.com/questions/139579/nginx-subdomain-rewrite

答案 1 :(得分:2)

server {
  listen 80;
  listen 443;
  server_name ~^www\.(\w+)\.domain\.com$;
  location / {
    rewrite ^ $scheme://$1.domain.com$request_uri? permanent;
  }
}