Domain name, port and IP of Nginx virtual host

For nginx source package, please send me a private message

Domain name of Nginx virtual host

[root@localhost ~]# yum install bind -y

Configure DNS master profile

[root@localhost ~]# vim /etc/named.conf 

options {
        listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        recursing-file  "/var/named/data/named.recursing";
        secroots-file   "/var/named/data/named.secroots";
        allow-query     { any; };

Configure zone profiles

[root@localhost ~]# vim /etc/named.rfc1912.zones 

zone "cwq.com" IN {
        type master;
        file "cwq.com.zone";
        allow-update { none; };
};
zone "kgc.com" IN {        
        type master;
        file "kgc.com.zone";
        allow-update { none; };
};

zone "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.

Configure zone data profile

[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost cwq.com.zone
[root@localhost named]# vim cwq.com.zone 
$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.136.162

[root@localhost named]# cp -p cwq.com.zone kgc.com.zone 

Turn on DNS Service

[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service 
[root@localhost named]# setenforce 0

Go to win10 to test if you can resolve the domain name

Install nginx environment package

[root@localhost named]# yum install gcc gcc-c++ pcre-devel zlib-devel -y

[root@localhost named]# mkdir /abc
[root@localhost named]# mount.cifs //192.168.100.23/LNMP /abc
Password for root@//192.168.100.23/LNMP:  
[root@localhost named]# cd /abc/
[root@localhost abc]# ls
mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz
nginx-1.12.0.tar.gz        php-7.1.10.tar.bz2
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/

Compilation and installation

[root@localhost abc]# cd /opt/
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx  #Create a program user to manage, - M has no home directory, - s does not log in to the local console
./configure \
--prefix=/usr/local/nginx \  #Specified path
--user=nginx \  #Designated user
--group=nginx \  #Specified group
--with-http_stub_status_module #Status statistics module

[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install

When it compiles, we open another terminal to create web pages

[root@localhost ~]# mkdir -p /var/www/html/cwq
[root@localhost ~]# mkdir -p /var/www/html/kgc
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
cwq  kgc
[root@localhost html]# echo "this is cwq web" > cwq/index.html
[root@localhost html]# echo "this is kgc web" > kgc/index.html
[root@localhost html]# ls cwq/
index.html
[root@localhost html]# ls kgc/
index.html

Do soft links, test syntax

[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.12.2]# 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

Write nginx script and put it in system startup script for service manager management

[root@localhost nginx-1.12.2]# cd /etc/init.d/

[root@localhost init.d]# vim nginx  

#!/bin/bash
#chkconfig: - 99 20  #Annotation information
#description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"  #This variable points to my command file
PIDF="/usr/local/nginx/logs/nginx.pid"  #This variable points to the process number of nginx
case "$1" in
    start)
        $PROG                                              
        ;;
    stop)
        kill -s QUIT $(cat $PIDF) 
        ;;
    restart)                                                  
        $0 stop
        $0 start
        ;;
    reload)                                                  
        kill -s HUP $(cat $PIDF)
        ;;
    *)                                                           
                echo "Usage: $0 {start|stop|restart|reload}"
                exit 1
esac
exit 0

[root@localhost init.d]# chmod +x nginx  #Add execution permission
[root@localhost init.d]# chkconfig --add nginx #Add nginx
[root@localhost init.d]# service nginx start 
[root@localhost init.d]# netstat -ntap | grep nginx  #Check whether nginx port is provided
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LIST
EN      42982/nginx: master

We need different domain names to visit different web pages

Edit nginx profile

[root@localhost init.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

#Delete all other superfluous items in the sever area
server {
         listen       80;
 33         server_name  www.cwq.com;  #domain name
 34         charset utf-8;  #Recognize Chinese character set
 35         access_log  logs/www.cwq.com.access.log; #Domain name log file
 36         location / {
 37             root   /var/www/html/cwq;  #Designated site
 38             index  index.html index.htm;
 39         }   
 40         error_page   500 502 503 504  /50x.html;
 41         location = /50x.html {
 42             root   html;
 43         }   
 44     }   
 45     
 46     
47     server {
 48         listen       80;
 49         server_name  www.kgc.com;
 50         charset utf-8;
 51         access_log  logs/www.kgc.com.access.log;
 52         location / {
 53             root   /var/www/html/kgc;
 54             index  index.html index.htm;
 55         }
 56         error_page   500 502 503 504  /50x.html;
 57         location = /50x.html {
 58             root   html;
 59         }
 60     }
 61     # another virtual host using mix of IP-, name-, and port-based c    onfiguration
#The superfluous content has been deleted to this place

Check syntax, restart service

[root@localhost conf]# 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@localhost conf]# service nginx restart

Go to win10 and test again


Port based of Nginx virtual host

Write a web page for port 8080

[root@localhost conf]# cd /var/www/html/
[root@localhost html]# mkdir kgc8080
[root@localhost html]# echo "this is kgc8080 web" > kgc8080/index.html

Configuring nginx files

[root@localhost html]# cd /usr/local/nginx/conf/

#First of all, we will cwq Comment out of domain name, 31, 44 s/^/#/g, or it will conflict with the following address
31 #    server {
  #        listen       80;
 33 #        server_name  www.cwq.com;
 34 #        charset utf-8;
 35 #        access_log  logs/www.cwq.com.access.log;
 36 #        location / {
 37 #            root   /var/www/html/cwq;
 38 #            index  index.html index.htm;
 39 #        }  
 40 #        error_page   500 502 503 504  /50x.html;
 41 #        location = /50x.html {
 42 #            root   html;
 43 #        }  
 44 #    }  

46     server {
 47         listen       192.168.136.162:80;
 48         server_name  www.kgc.com;
 49         charset utf-8;
 50         access_log  logs/www.kgc.com.access.log;
 51         location / {
 52             root   /var/www/html/kgc;
 53             index  index.html index.htm;
 54         }   
 55         error_page   500 502 503 504  /50x.html;
 56         location = /50x.html {
 57             root   html;
 58         }   
 59     }   
 60  
 61     server {
 62         listen       192.168.136.162:8080;
 63         server_name  www.kgc.com;
 64         charset utf-8;
 65         access_log  logs/www.kgc8080.com.access.log;  #Log files for different ports
 66         location / {
 67             root   /var/www/html/kgc8080;  #Sites on different ports
 68             index  index.html index.htm;
 69         }
 70         error_page   500 502 503 504  /50x.html;
 71         location = /50x.html {
 72             root   html;
 73         }
 74     }

Check the syntax, restart the service, and check whether the two ports are provided

[root@localhost conf]# 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@localhost conf]# service nginx restart
[root@localhost conf]# netstat -ntap | grep nginx
tcp        0      0 192.168.136.162:8080    0.0.0.0:*               LISTEN      43616/nginx: master 
tcp        0      0 192.168.136.162:80      0.0.0.0:*               LISTEN      43616/nginx: master 

Go to win10 for a test

This is the address of my second network card

IP based address of Nginx virtual host

Let's add a network card to the server first

This is the address of my second network card

ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.136.171  netmask 255.255.255.0  broadcast 192.168.136.255

Change the address of kgc area data configuration file

[root@localhost ~]# cd /var/named/
[root@localhost named]# vim kgc.com.zone 

$TTL 1D
@       IN SOA  @ rname.invalid. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
        NS      @
        A       127.0.0.1
www IN  A       192.168.136.171

[root@localhost named]# systemctl restart named

Test whether domain name can be resolved successfully with win10

Configure nginx profile

[root@localhost named]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
#Comment out the previous port, 61, 74 s/^/#/g
 61 #    server {
 62 #        listen       192.168.136.162:8080;
 63 #        server_name  www.kgc.com;
 64 #        charset utf-8;
 65 #        access_log  logs/www.kgc8080.com.access.log;
 66 #        location / {
 67 #            root   /var/www/html/kgc8080;
 68 #            index  index.html index.htm;
 69 #        }
 70 #        error_page   500 502 503 504  /50x.html;
 71 #        location = /50x.html {
 72 #            root   html;
 73 #        }
 74 #    }
#Remove the previous comment, 31, 44 s/#//g
31     server {
        listen       192.168.136.162:80;
 33         server_name  www.cwq.com;
 34         charset utf-8;
 35         access_log  logs/www.cwq.com.access.log;
 36         location / {
 37             root   /var/www/html/cwq;
 38             index  index.html index.htm;
 39         }
 40         error_page   500 502 503 504  /50x.html;
 41         location = /50x.html {
 42             root   html;
 43         }
 44     }
#Change the area monitoring address of kgc to the address of the second network card
 47         listen       192.168.136.171:80;

Check syntax, restart service

[root@localhost conf]# 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@localhost conf]# service nginx restart 

Go to win10 again to test whether different websites (domain names) get the same web page


That's all we have. Thank you for watching

Tags: Nginx vim network yum

Posted on Thu, 07 Nov 2019 05:11:25 -0500 by BSlepkov