Nginx summary (two) ip-based virtual host configuration

Speaking in front of how to install configure Nginx, you can go here to see nginx series: https://www.cnblogs.com/zhangweizhong/category/1529997.html

I say today is how to configure Nginx virtual host.

 

1. What is Web Hosting

         Hosting is a special hardware and software technology, it can be every computer on the network into a plurality of virtual hosts, each virtual host can independently provide external www services, this can be achieved to provide a plurality of external host web service, are independent between each virtual host, affect each other.

As shown below:

 

nginx virtual host configuration can be achieved by, nginx supports three types of virtual host configuration:

1, ip-based virtual hosts

2, name-based virtual hosting

3, Port-based virtual hosts

Actual use, we use the domain name is commonly used to distinguish between web service or port. I'm just here to say that overall, will talk about all three configurations. 

 

2. nginx configuration file structure

nginx configuration file structure is as follows:

......

events {

    .......

}

http{

   .......

   server{

         .......

         }

   server{

         .......

         }
}

Each server is a virtual host.

 

3. ip-based virtual host configuration

         Linux operating system allows you to add IP alias is bound to multiple lP address on one physical NIC. This makes it possible to run multiple IP-based virtual hosts on the same server using a single network card.

Scenarios

A nginx server bind two ip: 192.168.78.132,192.168.78.133, different html directory different ip access request, namely:

Access http://192.168.78.132 access html pages directory under html132

Access http://192.168.78.133 access html pages directory under html133

 

i. Prepare the environment

Create a virtual machine 192.168.78.132 ensure local computer and the virtual network open.

Install nginx on 192.168.78.132. Talked about this before nginx source installation, you can go and see this article: https://www.cnblogs.com/zhangweizhong/p/11378512.html

 

ii. Binding multi-ip

1, modify the network configuration file into the /etc/sysconfig/network-scriptsedit ifcfg-ens33file is as follows:

TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
# BOOTPROTO=dhcp 注意区别!!!
DEFROUTE=yes
IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=26c2f3f8-62c5-4571-80e2-ca394cfd43da DEVICE=ens33 ONBOOT=yes ZONE=public # 注意区别 IPADDR0=192.168.78.132 PREFIX0=24 IPADDR1=192.168.78.133 PREFIX1=16

 

2, save the changes, restart the network:  systemctl restart Network

[root@bogon network-scripts]# systemctl restart network

This is a reference to the online information, do not understand can go here to see exactly how: https://blog.csdn.net/u013887008/article/details/79589656 

 

iii. Create two web sites

Enter / usr / local / nginx catalog, the original html directory of nginx two copies of the directory "html132" and "html133", in order to facilitate testing index.html need to modify the contents of each directory to make it personalized.

cd /usr/local/nginx

cp -r html html132
cp -r html html133

 

iv. Virtual Host Configuration

Modify /usr/local/nginx/conf/nginx.conf file, add two virtual hosts, as follows:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
 
    sendfile        on;

    keepalive_timeout  65;

    #配置虚拟主机192.168.78.132
    server {
        # Ip and listening port configuration 192. 
             # specify the welcome page and look left to right order168.78. 132 : 80 
        the listen        80 ; 
        # virtual host name where the ip address 

        server_name   192.168 . 78.132 ; 

        # all requests to / start, all requests are matched with this LOCATION 
        LOCATION / { 
             # using the root directive specifies the virtual host directory i.e. page storage directory 
             # such as access to HTTP: // ip / test.html will find /usr/local/html3/test.html 
             # such as access to HTTP: // ip / Item / test.html will find / usr / local / html3 / item /test.html 
             the root / usr / local / Nginx / html132; 
    }

             index.html index.htm index; 
        }

    # Virtual host configuration 192. 168.78 . 133 
    Server { 
        the listen        80 ; 
        server_name   192.168 . 78.133 ; 
        LOCATION / { 
            the root    / usr / local / Nginx / html133; 
            index index.html index.htm; 
        } 
    } 
}

 

v. Test

 Restart nginx, observe the port monitoring status:

Access http://192.168.78.132/

 

Access http://192.168.78.133/

 

 

4. Finally

Or more, put nginx finished based on the configuration of the virtual hosts ip. Behind will continue to speak based on the configuration of the domain name and port.

 

Guess you like

Origin www.cnblogs.com/zhangweizhong/p/11378542.html