Nginx的域名重定向与httpd类似。
vim /usr/local/nginx/conf/vhost/moved.conf
server
{
listen 80;
#nginx可以配置多个主机名,apache只能使用ServerAlias来指定别名
server_name testmoved.com testmoved2.com testmoved3.com;
index index.html index.htm index.php;
root /data/wwwroot/testmoved;
#判断host是否为test.com
if ($host != 'testmoved.com')
{
rewrite ^/(.*)$ http://testmoved.com/$1 permanent;
}
}
/usr/local/nginx/sbin/nginx -t
(ctrl+r 后输入 -t可以把上一次输入带有-t的命令列出来)
/usr/local/nginx/sbin/nginx -s reload
mkdir /data/wwwroot/testmoved
echo "重新定向测试成功" > /data/wwwroot/testmoved/index.html
curl -x127.0.0.1:80 testmoved.com/
curl -x127.0.0.1:80 testmoved2.com/ -I
扩展
nginx.conf 配置详解:
http://www.ha97.com/5194.html
http://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四种flag:
http://www.netingcn.com/nginx-rewrite-flag.html http://unixman.blog.51cto.com/10163040/1711943