修改虚拟主机配置文件
[root@dl-001 default]# vim /usr/local/nginx/conf/vhost/test.com.conf
server
{
listen 80;
//nginx可以配置多个主机名,apache只能使用ServerAlias来指定别名
server_name test.com test2.com;
index index.html index.htm index.php;
root /data/www/test.com;
// 在多个域名
//判断host是否为test.com
if ($host != 'test.com') {
rewrite ^/(.*)$ http://test.com/$1 permanent;
}
}
检测并重新加载
[root@dl-001 default]# /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@dl-001 default]# /usr/local/nginx/sbin/nginx -s reload
检测
[root@dl-001 default]# curl -x 127.0.0.1:80 test2.com/index.html
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@dl-001 default]# curl -x 127.0.0.1:80 test2.com/admin/index.html
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
[root@dl-001 default]# curl -x 127.0.0.1:80 test3.com/index.html
aaa.com