Nginx域名跳转
Nginx域名重定向多用在一个网站的多个域名重定向到同一个域名
一、修改Nginx虚拟主机配置文件,添加重定向配置
[root@daixuan vhosts]# pwd
/usr/local/nginx/conf/vhosts
[root@daixuan vhosts]# vim test.conf
server
{
listen 80;
server_name www.test.com www.aaa.com www.bbb.com;
if ($host != 'www.test.com'){
rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
index index.html index.htm index.php;
root /data/www;
location ~ .*admin\.php$ {
auth_basic "daixuan auth";
auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
}
二、测试域名重定向
1、windows的hosts文件中添加192.168.101.230 www.test.com www.aaa.com www.bbb.com
2、检查配置文件有没有错误
[root@daixuan vhosts]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@daixuan vhosts]# /etc/init.d/nginx reload
重新载入 Nginx: [确定]
3、测试
[root@daixuan vhosts]# curl -x127.0.0.1:80 www.aaa.com
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>
在浏览器中输入www.aaa.com自动跳转到www.test.com