nginx将子域redirect到一个文件夹

在Nginx中,我想将我的subdomain.domain.comredirect到domain.com/sub/。我该怎么做? 我想为domain.com使用现有的站点configuration文件,而不是创build一个新的。

我有的configuration文件是(如何添加到此)

======

server { listen 80; server_name www.domain.com domain.com *.domain.com; access_log /srv/www/domain.com/logs/access.log; error_log /srv/www/domain.com/logs/error.log; location / { root /srv/www/domain.com/public_html; index index.html index.htm; } 

location / phpmyadmin {root / usr / share; index index.php; }

 location ~ \.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /srv/www/domain.com/public_html$fastcgi_script_name; } 

}

=========

对不起,迈克,你的if语句解决scheme是一个坏主意…请参阅Nginx 陷阱维基页面为什么(在这种情况下,nginx将评估每个请求的指令,这是低效的)…

在包含此项的人之上添加另一个服务器块:

 server { server_name subdomain.domain.com; rewrite ^ $scheme://domain.org/subdomain$request_uri redirect; } 

魔术就会完成

这将在你的服务器块。 这是未经testing,但应该工作或给你一个想法

 if ($host ~* "^(.*)\.domain\.com$"){ set $subd $1; rewrite ^(.*)$ http://domain.com/$subd permanent; break; }