Using Nginx to implement virtual host under Centos7.2

Source: Internet
Author: User
First, preface

First of all, you need to make sure that the Nginx is properly installed on your Linux system, but if you do not install Nginx, please refer to

System environment:
Linux Environment: centos-7.2
Nginx Environment: nginx-1.9.9

Second, about Nginx
Nginx is a high-performance HTTP server/reverse proxy server and e-mail (IMAP/POP3) proxy server. Developed by the Russian program designer Igor Sysoev, the official test Nginx can support 50,000 concurrent links, and the CPU, memory and other resource consumption is very low, the operation is very stable. Open source, free.


Iii. what can I do with Nginx?
1, HTTP server: Nginx is an HTTP service can provide HTTP services independently. Can do web static server.
2, Virtual Host: can be implemented in a single server virtual out of multiple sites. For example, a personal web site uses a virtual host.
3, reverse proxy/load balancing: When the number of visits to a certain level of the site, a single server can not meet the user's request, you need to use more than one server cluster can be used to do reverse proxy. And more than one server can share the load evenly, not because of a server load high downtime and a server idle situation.


Iv. using Nginx to implement virtual host
Here we need to know why we need to create a virtual host, in the actual production environment, our business is accessed through the public network,
I am building a cloud server, a cloud server corresponding to a public IP, so the public IP for the general company is very scarce resources,
For some large companies, such as BAT, it might not matter.
So here using Nginx to implement virtual host, can be implemented in the same service to run multiple sites, and the site does not interfere with each other.


The same server may have an IP, the site needs to use port 80, but the domain name of the site is different.
There are three ways to differentiate between different websites:

1. HTTP Service
2. Implement Virtual Machine
1) IP-based virtual machines
2) port-based virtual machines
3) domain-based virtual machines
3, reverse proxy, load balancing

Five, IP-differentiated virtual host

Bind multiple IP addresses on a single server.
Method One:
Add LP aliases using standard network configuration tools such as Ifconfig and route commands.

Enter the command "Ifconfig" To view the current IP configuration, such as:


The Ens33 NIC is then bound to a ip:192.168.78.142, and the NIC is named Ens33:1

/sbin/ifconfig ens33:1 192.168.78.142 broadcast 192.168.78.255 netmask 255.255.255.0 up/sbin/route add-host 192.168.78.142 Dev Ens33:1

After the virtual network card is created, such as:



Method Two:
1, copy the/etc/sysconfig/network-scripts/ifcfg-ens33 file,

Enter the directory, and the input command "CP Ifcfg-ens33 Ifcfg-ens33:1-R" is named Ifcfg-ens33:1, such as:


2, modify the configuration file, enter the command "VI ifcfg-ens33:1", modify the following content,

name=ens33:1device=ens33:1ipaddr=192.168.78.142

Other items do not need to be modified, after modification, such as:


After the creation is complete, test whether the new IP is bound successfully, enter the command "Ping 192.168.78.142" in the DOS window, such as:


Note: Using the method of a bound IP, after the system restarts, will automatically unbind, need to be re-bound, method two is permanent, this is the conclusion of the actual exercise.
3, restart the system,
Enter the command "reboot", restart the system and then enter the command "Ifconfig" can see the normal creation of a new network card,
Such as:

VI, NGINX virtual machine implementation

1), configure Nginx IP address-based virtual host
Prepare two Nginx-ID HTML for the test, good difference: Enter into the/usr/local/nginx directory, the HTML is copied two copies,

In modifying the following index.html content, here is not easy to write, if you do not know please leave a message or private messages, such as:


Modify Nginx configuration file, enter command "VI conf/nginx.config"

To facilitate the reader's copy, I am here to delete the unnecessary, the content is as follows:

    server {        listen       ;        server_name  192.168.78.141;        Location/{            root   html-141;            Index  index.html index.htm;        }    }    server {        listen       ;        server_name  192.168.78.142;        Location/{            root   html-142;            Index  index.html index.htm;        }    }

As shown in the following:


Test whether Nginx virtual host can be accessed normally,

Test the 192.168.78.141 virtual host, such as:




Test the 192.168.78.142 virtual host, such as:




2), configure Nginx Port-based virtual host
Or the same as the usual, prepare two of the HTML to identify Nginx, used in testing the good difference: into the/usr/local/nginx directory, the HTML is copied two copies,

In modifying the following index.html content, here is not easy to write, if you do not know please leave a message or private messages, such as:


Modify Nginx configuration file, enter command "VI conf/nginx.config"

To facilitate the reader's reproduction, the content is as follows:

server {        listen       Bayi;        server_name  192.168.78.141;        Location/{            root   html-81;            Index  index.html index.htm;        }    }    server {        listen       ;        server_name  192.168.78.141;        Location/{            root   html-82;            Index  index.html index.htm;        }    }

As shown in the following:


After restarting Nginx, test whether the Nginx virtual host can be accessed normally,

Test a 81-port virtual host, such as:


Test a 82-port virtual host, such as:





3), domain-based virtual host
Domain-based virtual hosts are the most useful way to configure a virtual host.
That is, a domain name can only be bound to one IP address, and an IP address may be bound by multiple domain names.


The implementation of domain-based virtual host, here also need to modify the Linux HostName, of course, can also be modified through this tool, for Internet developers, often change the host must be exempt. Every time we go over and over again to modify the hosts file is very tired, if can be more quickly modified into different hosts, here for everyone to recommend a good software switchhosts, easy one-click switch.

Open as Administrator, then you can set the domain name and IP mapping, add a local solution, type the following,

192.168.78.141 www.12345.com192.168.78.141 register.12345.com192.168.78.141 login.12345.com

Such as:




Note: Modify the Hosts file for window: (C:\WINDOWS\SYSTEM32\DRIVERS\ETC)


Based on the Nginx domain name of the virtual host configuration, modify content such as:


    server {        listen       ;        server_name  www.12345.com;        Location/{            root   html;            Index  index.html index.htm;        }    }    server {        listen       ;        server_name  register.12345.com;        Location/{            root   html-81;            Index  index.html index.htm;        }    }    server {        listen       ;        server_name  login.12345.com;        Location/{            root   html-82;            Index  index.html index.htm;        }    }

As shown in the following:



After modifying the configuration file, Nginx needs to reload the configuration file.

Test www.12345.com, such as:


Test register.12345.com, such as:


Test login.12345.com, such as:


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.