1、复制Nginx配置文件
[root@localhost ~]# cd /etc/nginx/conf.d/ #Nginx配置文件的路径
[root@localhost conf.d]# cp default.conf default1.conf
#Ngin的主配置文件路径是 /etc/nginx/下的 nginx.conf文件
2、修改配置文件 default.conf
[root@localhost conf.d]# vim default.conf
server {
listen 80; #监听服务的端口,也可以写成“IP:端口号”,Nginx默认监听80端口
server_name www.host.com; #设置服务名,也就是域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main; #默认的成功日志缓存的路径,默认注释
location / {
root /opt/app/code; #用root作为访问者,访问路径为/opt/app/code
index index.html index.htm; #访问的页面包括页面的html文件或者htm文件
}
3、修改配置文件 default1.conf
[root@localhost conf.d]# vim default1.conf
server {
listen 80;
server_name www.vincen.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /opt/app/code1;
index index.html index.htm;
}
4、在对应路径下创建相应的访问文件
[root@localhost conf.d]# cd /opt/app/code
[root@localhost code]# vim host.html
<html> #HTML语言的开头
<head>
<meta charset="utf-8"> #设置文本的字符集为utf-8
<title>host</title> #设置页面的标题
</head>
<h1>www.host.com</h1>
<h1>基于域名虚拟主机</h1>
</html>
[root@localhost conf.d]# cd /opt/app/code1
[root@localhost code]# vim vincen.html
<html>
<head>
<meta charset="utf-8">
<title>vincen</title>
</head>
<h1>www.vincen.com</h1>
<h1>基于域名虚拟主机</h1>
</html>
5、检查NGINX配置语法
[root@localhost code]# nginx -tc /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6、重新载入Nginx服务
[root@localhost code]# nginx -s reload -c /etc/nginx/nginx.conf
7、安装DNS服务
[root@localhost conf.d]# cd /root/
[root@localhost ~]# yum -y install bind
8、修改DNS的配置文件
[root@localhost ~]# vim /etc/named.conf
options {
directory "/var/named";
};
zone "host.com" IN {
type master;
file "host.com.zone";
};
zone "vincen.com" IN {
type master;
file "vincen.com.zone";
};
9、修改地址库文件
创建host.com.zone并修改配置信息,将域名解析到本机
[root@localhost ~]# vim /var/named/host.com.zone
$TTL 1D
host.com. IN SOA host.com. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
host.com. IN NS dns.host.com.
dns.host.com. IN A 172.25.0.1
www.host.com. IN A 172.25.0.1
创建vincen.com.zone并修改配置信息,将域名解析到本机
[root@localhost ~]# vim /var/named/vincen.com.zone
$TTL 1D
vincen.com. IN SOA vincen.com. rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
vincen.com. IN NS dns.vincen.com.
dns.vincen.com. IN A 172.25.0.1
www.vincen.com. IN A 172.25.0.1
10、启动DNS服务
[root@localhost named]# systemctl start named
11、Linux上测试域名解析到虚拟机IP (断开物理机的网络连接)
解析 www.host.com
[root@localhost named]# nslookup www.host.com
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: www.host.com
Address: 172.25.0.1
解析 www.vincen.com
[root@localhost named]# nslookup www.vincen.com
Server: 127.0.0.1
Address: 127.0.0.1#53
Name: www.vincen.com
Address: 172.25.0.1
12、设置物理机网络适配器,将物理机连接Linux的网卡中的DNS指向Linux
13、在浏览器访问