Nginx virtual host based on host domain name

Keywords: Operation & Maintenance Nginx DNS vim Linux

1. Copy the Nginx configuration file

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# cp default.conf default1.conf

2. Modify the configuration file default.conf

[root@localhost conf.d]# vim default.conf
server {
    listen       80;
    server_name  www.host.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /opt/app/code;
        index  index.html index.htm;
    }

3. Modify the configuration file 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. Create the corresponding access file under the corresponding path

[root@localhost conf.d]# cd /opt/app/code
[root@localhost code]# vim host.html 
<html>
<head>
        <meta charset="utf-8">
        <title>host</title>
</head>
<body style="backgroup-color:yellow;">
<h1>www.host.com</h1>
<h1>Domain name based virtual host</h1>
</body>
</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>
<body style="backgroup-color:yellow;">
<h1>www.vincen.com</h1>
<h1>Domain name based virtual host</h1>
</body>
</html>

5. Check NGINX configuration syntax

[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. Reload Nginx service

[root@localhost code]# nginx -s reload -c /etc/nginx/nginx.conf

7. Install DNS Service

[root@localhost conf.d]# cd /root/
[root@localhost ~]# yum -y install bind

8. Modify DNS configuration file

[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. Modify address library file

Create host.com.zone and modify the configuration information to resolve the domain name to the local computer

[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

Create vincen.com.zone and modify the configuration information to resolve the domain name to the local computer

[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. Start DNS Service

[root@localhost named]# systemctl start named

11. On Linux, the test domain name resolves to the virtual machine IP (disconnect the network connection of the physical machine)

Analysis of 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

Analysis of 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. Set the network adapter of the physical machine, and point the DNS in the network card of the physical machine connected to Linux to Linux

13. Access in browser

Posted by Dilb on Fri, 13 Dec 2019 09:21:38 -0800