nginx 多域名自动转向主域名

很多时候,出于域名保护的考虑,一个网站会拥有多个域名,如:

www.test.com

www.test.com.cn

test.com

test.com.cn

实际上,一般以上四个域名实际上都是指向同一个IP的同一套应用程序。

为了方便程序会话的管理(session  cookie 是关联当前域名的),用户访问其中任何一个域名时,我们都会将其自动转向其中的一个主打域名,比如 www.test.com.

在nginx中,需要在nginx.conf文件中,添加类似如下配置,实现上述效果。

server
	{
		listen       80;
		server_name www.test.com www.test.com.cn;
		index index.html index.htm index.php;
		root /home/wwwroot;

		if ($host = 'www.test.com.cn' ) {
				rewrite ^/(.*)$ http://www.test.com/$1 permanent;
		}
		if ($host = 'test.com' ) {
				rewrite ^/(.*)$ http://www.test.com/$1 permanent;
		}
		if ($host = 'test.com.cn' ) {
				rewrite ^/(.*)$ http://www.test.com/$1 permanent;
		}

猜你喜欢

转载自huangqiqing123.iteye.com/blog/1969727
今日推荐