Nginx: Virtual Host Configuration

Source: Internet
Author: User
Tags nginx server

Continue my Nginx learning journey today and talk about the configuration of the virtual host. What is a virtual host? Virtual host uses a special hardware and software technology, it is running on the Internet server host into a "virtual" host, each virtual host can be a separate Web site, can have a separate domain name, with complete Internet server functions, The virtual machines on the same host are completely independent. The virtual host provides the ability to run multiple sites on the same server, on the same set of nginx processes, without having to provide a separate Nginx server for each Web site to run or to run a set of nginx processes separately. Like Apache, Nginx can also configure a variety of types of virtual host, one is based on IP virtual host, the second is based on the domain name of the virtual host, and the third is a port-based virtual host.

(i) Configure IP-based virtual hosts

The Linux operating system allows the addition of IP aliases, which can bind multiple IP addresses on a physical network card. This enables multiple IP-based virtual hosts to be run on the same server that uses a single network card. Setting the IP alias is also very simple, just configure the NIC interface on the system and let it listen for additional IP addresses. On Linux systems, you can add IP aliases using standard network configuration tools such as the ifconfig and route commands. Use the Ifconfig command to view the IP address of the server.

The local loopback represents the virtual interface of the device, so the default is to be seen as an interface that never goes down, its main role is two: one is to test the network configuration, can ping the 127.0.0.1 to explain that the LAN and IP protocol installation of the machine is not a problem; Client applications must invoke resources on the server at run time, typically specifying the IP address of the server, but when the program is running on the same machine and there is no other server, the server's resources can be mounted on the machine. The server's IP address is set to 127.0.0.1 and can also be run.


If you want to add an IP alias on the ETH0 network card device 192.168.1.223 can be done through the ifconfig and route commands:

Ifconfig eth0:0 192.168.1.223 broadcast 192.168.1.255 netmask 255.255.255.0 up

Or

Route add-host 192.168.1.223 Dev eth0:0


Eth0:0 represents the 1th virtual IP of the Eth0 network card, of course, you can also open a second third, in turn, eth0:1,eth0:2

Broadcast followed by a broadcast address.

Netmask is followed by a subnet mask.

UP indicates activation

Device name that is represented behind Dev

If the above instruction is not successful, see if it is a permissions issue, switch to root and try


IP aliases configured with ifconfig and route will disappear after the server restarts and can be added to the/etc/rc.local file by adding these two ifconfigt and route commands, allowing the system to run automatically when it is powered on

/sbin/ifconfig eth0:0 192.168.1.223 broadcast 192.168.1.255 netmask 255.255.255.0 up

Or

/sbin/route add-host 192.168.1.223 Dev eth0:0

Why is/sbin/ifconfig. Because the executable files for these commands are in the/sbin directory, you can view them with the Whereis command.


The following commands are available for clearing virtual IP:

Ifconfig eth0:x Down

x represents the interface of the virtual network.

Or

IP addr del v_ip dev eth0

V_IP represents the virtual IP that is created


Construction has created two virtual IPs, respectively, 192.168.8.16,192.168.8.18, below to see how to configure both virtual IP

http {      include       mime.types;   default_type  application/octet-stream;  keepalive_timeout  65;   access_log  log/server1.access.log combined;    server {               listen    192.168.8.16:80;    #监听的IP和端口                     server_name  192.168.8.16;   #主机名称                         location / {                                             index  index.html index.htm;                   root / data0/hrdocs/server1;                     }           }           server {                   listen   192.168.8.18:80;                    server_name  192.168.8.18;                     location / {                                        index  index.html index.htm;              root /data0/hrdocs/server1;               }   }

A server{...} is a virtual host, if the configuration of multiple virtual hosts, establish a multi-segment server{} configuration can be very convenient, listening to the IP and port, can also not write IP address, write-only port, it is configured as "Listen 80", Indicates that 80 ports listening on all IPs on the server can differentiate between different virtual hosts by server_name


(ii) domain-based virtual hosting

Domain-based virtual hosts are the most common type of virtual hosts. Simply configure your DNS server, map each hostname to the correct LP address, and then configure the Nginx server so that it can recognize different host names. This virtual host technology, so that many virtual hosts can share the same LP address, effectively solve the problem of insufficient LP address. So, if there is no special requirement that you have to use a virtual host based on LP, it is best to use a domain-based virtual host.

We can use the edit/etc/hosts to join the virtual domain to parse, to test. In the Hosts file, add

127.0.0.2 www.test.com

Such as:

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8B/3E/wKioL1hIDFCTwcGaAACLpfA6iWk388.png-wh_500x0-wm_3 -wmp_4-s_776754281.png "title=" Selection _013.png "alt=" Wkiol1hidfctwcgaaaclpfa6iwk388.png-wh_50 "/>


Then save the exit and execute the following command to restart the contents of the file

Source hosts

Then ping the command to do a little test, see 127.0.0.2 Pass

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/8B/42/wKiom1hIDRTyh3NqAAGaZ4fGEl8482.png-wh_500x0-wm_3 -wmp_4-s_2762302557.png "title=" Selection _015.png "alt=" Wkiom1hidrtyh3nqaagaz4fgel8482.png-wh_50 "/>

In terms of results, ping 127.0.0.2 and www.test.com are all in flux. Next, configure Nginx. The configuration is as follows:

server {Listen 80;    server_name www.test.com;      Location/{root/home/liulonghua/download/confusion; index index.html; #当然如果你有更好的模板HTML文件, can also change, change the root path on the line}}

Then save exit, test the configuration with nginx-t error, execute the following command to restart Nginx

Service Nginx Restart

Then open the browser and enter the website www.test.com. It's my test results.

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8B/42/wKiom1hID1CS0u84AADws4JOjIs743.png-wh_500x0-wm_3 -wmp_4-s_2336498021.png "title=" Selection _016.png "alt=" Wkiom1hid1cs0u84aadws4jojis743.png-wh_50 "/>

Here is just a simple example, the actual production must be more complicated than this.


(iii) Port-based virtual host configuration

Use port to differentiate, browser use domain name or IP address: port number

Look at the following configuration:

server{listen 8080;        server_name www.test.com;        Location/{root/home/liulonghua/download/confusion;        Index index.html;}}        server{Listen 9090;        server_name 127.0.0.1;        Location/{root/usr/share/nginx/html; Index index.html;}}

This time through the browser access to the www.test.com or 127.0.0.1 Web page is not open, you must add the port after the line, interested can try their own hands.

Reference: "Real-combat Nginx"

Nginx: Virtual Host configuration

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.