Nginx virtual host configuration of three ways of based on IP

  • 2020-05-15 03:34:09
  • OfStack

Nginx configuration virtual host support three ways: based on IP virtual host configuration, port-based virtual host configuration, virtual host configuration based on domain name.

Rounding Nginx three kinds of virtual host configuration (port) based https: / / www ofstack. com article / 14977. htm

Rounding Nginx three kinds of virtual host configuration (based on the domain name) https: / / www ofstack. com article / 14978. htm

1. Virtual host configuration based on IP

If you have multiple IP on the same server, you can use an IP-based virtual host configuration to bind different services to different IP.

1.1 suppose the server has an IP address of 192.168.2.150. First, ifconfig is used to bind the other three IP on the same network interface.


[root@localhost ~]# ifconfig ens33:1 192.168.2.151/24 up
[root@localhost ~]# ifconfig ens33:2 192.168.2.152/24 up
[root@localhost ~]# ifconfig ens33:3 192.168.2.153/24 up
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.2.106 netmask 255.255.255.0 broadcast 192.168.2.255
 inet6 fe80::2a8d:be6:a4a8:ea0 prefixlen 64 scopeid 0x20<link>
 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)
 RX packets 1220 bytes 87955 (85.8 KiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 206 bytes 23755 (23.1 KiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.2.151 netmask 255.255.255.0 broadcast 192.168.2.255
 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.2.152 netmask 255.255.255.0 broadcast 192.168.2.255
 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

ens33:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.2.153 netmask 255.255.255.0 broadcast 192.168.2.255
 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
 inet 127.0.0.1 netmask 255.0.0.0
 inet6 ::1 prefixlen 128 scopeid 0x10<host>
 loop txqueuelen 1 (Local Loopback)
 RX packets 72 bytes 6252 (6.1 KiB)
 RX errors 0 dropped 0 overruns 0 frame 0
 TX packets 72 bytes 6252 (6.1 KiB)
 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

1.2 the corresponding domain names of the three IP are as follows. The host file of the host is configured for easy testing


[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1  localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.151 www.test151.com
192.168.2.152 www.test152.com
192.168.2.153 www.test153.com

You can simulate the implementation of DNS polling.

Attachment: after setting up the hosts file 1 must remember to execute the following command to make it effective

1, windows cmd into the command line


C:\Users\1234>ipconfig /flushdns

Windows IP  configuration 

 Successfully refreshed  DNS  Resolve the cache. 

1.3 establish the root directory of the web host and create the home page file index.html


[root@localhost /]# mkdir -p /data/www
[root@localhost /]# cd /data/www
[root@localhost www]# mkdir 151
[root@localhost www]# mkdir 152
[root@localhost www]# mkdir 153
[root@localhost www]# echo "192.168.2.151" > 151/index.html
[root@localhost www]# echo "192.168.2.152" > 152/index.html
[root@localhost www]# echo "192.168.2.153" > 153/index.html
[root@localhost www]# ls
151 152 153

1.4 modify nginx.conf to include the virtual host configuration file in the master file


[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf  fastcgi_params  koi-utf mime.types  nginx.conf  scgi_params  uwsgi_params  win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@localhost conf]# vim nginx.conf

Add the following configuration to the end of the nginx.conf file


#  in http Find the following and delete the" # " 
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
   '$status $body_bytes_sent "$http_referer" '
   '"$http_user_agent" "$http_x_forwarded_for"';

#  The end of the configuration file 1 A" } Add the following statement before ", as shown below 
include vhost/*.conf;
}

1.5 edit each IP profile (profile for each virtual host)


[root@localhost conf]# mkdir -p vhost
[root@localhost conf]# cd vhost/
[root@localhost vhost]# cat www.test151.conf
 server {
 listen 192.168.2.151:80;
 #  Configured to the actual domain name, the profile domain name is the same for each virtual host 
 #server_name www.test.com;

 access_log /data/logs/www.test151.com.log main;
 error_log /data/logs/www.test151.com.error.log;

 location / {
  root /data/www/151;
  index index.html index.htm;
 }
 }

[root@localhost vhost]# cat www.test152.conf
 server {
 listen 192.168.2.152:80;
 #  Configured to the actual domain name, the profile domain name is the same for each virtual host 
 #server_name www.test.com;

 access_log /data/logs/www.test152.com.log main;
 error_log /data/logs/www.test152.com.error.log;

 location / {
  root /data/www/152;
  index index.html index.htm;
 }
 }

[root@localhost vhost]# cat www.test153.conf
 server {
 listen 192.168.2.153:80;
 #  Configured to the actual domain name, the profile domain name is the same for each virtual host 
 #server_name www.test.com;

 access_log /data/logs/www.test153.com.log main;
 error_log /data/logs/www.test153.com.error.log;

 location / {
  root /data/www/153;
  index index.html index.htm;
 }
 }

1.6 create the log file, otherwise nginx cannot be started


[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.test151.com.log
[root@localhost /]# touch /data/logs/www.test151.com.error.log
[root@localhost /]# touch /data/logs/www.test152.com.log
[root@localhost /]# touch /data/logs/www.test152.com.error.log
[root@localhost /]# touch /data/logs/www.test153.com.log
[root@localhost /]# touch /data/logs/www.test153.com.error.log
[root@localhost /]# ls /data/logs/
www.test151.com.error.log www.test152.com.error.log www.test153.com.error.log
www.test151.com.log www.test152.com.log www.test153.com.log

1.7 test the configuration file before starting nginx


[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost 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
#  Start the nginx
[root@localhost sbin]# ./nginx

1.8 test files


[root@localhost sbin]# curl www.test151.com
192.168.2.151
[root@localhost sbin]# curl www.test152.com
192.168.2.152
[root@localhost sbin]# curl www.test153.com
192.168.2.153

Note: problems in the configuration process

1. Problems in testing configuration files


[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1  localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.151 www.test151.com
192.168.2.152 www.test152.com
192.168.2.153 www.test153.com
0

Solution: forget the semicolon in the following statement


[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1  localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.151 www.test151.com
192.168.2.152 www.test152.com
192.168.2.153 www.test153.com
1

2. Always access the same results when testing with curl www.test*.com

Solution: do not write the IP address after server_name, server_name can only be followed by domain names.


Related articles: