Build nginx virtual host based on domain name port and IP

  • 2020-05-17 07:52:50
  • OfStack

There are three types of virtual hosts supported by nginx

1, based on the domain name of the virtual host

2. Virtual host based on IP

3. Virtual host based on port

1. Build based on domain name

1. Compile and install nginx service

2. Configure DNS domain name resolution service

3. Configure the virtual host

a, create a self-test web page


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

b, edit the nginx.conf configuration file


vim /usr/local/nginx/conf/nginx.conf
 include conf.d/*.conf;
 server {
  listen    80;
  server_name www.kgc.com;
  charset utf-8;
  access_log logs/www.kgc.com.access.log ;
  location / {
   root /var/www/html/kgc;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
 server {
  listen    80;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.com.access.log ;
  location / {
   root /var/www/html/accp;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }

c, reload service


systemctl restart nginx
netstat -ntap | grep 80

d, access test

www.kgc.com
www.accp.com

2. Port-based

a, create another port test page


[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "this is kgc 8080 web" > kgc/index.html

b, edit nginx.conf configuration file, only modify the listening address


server {
  listen    192.168.109.137:80;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.com.access.log ;
  location / {
   root /var/www/html/accp;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
 server {
  listen    192.168.109.137:8080;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp8080.com.access.log ;
  location / {
   root /var/www/html/accp8080;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }

c, reload nginx service


systemctl restart nginx
netstat -ntap | grep 80

d, test page

www.accp.com
www.accp.com8080

3. Based on IP

1. Modify the regional data configuration file in the webpage configuration file


vim /var/named/kgc.com.zone
systemctl restart named

2. Edit the configuration in nginx.conf and modify the ip address


server {
  listen    192.168.109.137:80;
  server_name www.kgc.com;
  charset utf-8;
  access_log logs/www.kgc.com.access.log ;
  location / {
   root /var/www/html/kgc;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }
 server {
  listen    192.168.109.134:80;
  server_name www.accp.com;
  charset utf-8;
  access_log logs/www.accp.com.access.log ;
  location / {
   root /var/www/html/accp;
   index index.html index.htm;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
   root html;
  }
 }

c, overloaded nginx service


systemctl restart nginx
netstat -ntap | grep 80

d, test page


192.168.109.137
192.168.109.134

conclusion


Related articles: