首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 其他相关 >

nginx 多域名自动转为主域名

2013-11-14 
nginx 多域名自动转向主域名?很多时候,出于域名保护的考虑,一个网站会拥有多个域名,如:www.test.comwww.te

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;}

?

热点排行