Deploy IP-based virtual hosts in Nginx in Centos

Source: Internet
Author: User

I. Virtual Host Concept

Virtual Hosts allocate a certain amount of disk space on network servers for users to place sites and application components, and provide necessary site functions, data storage and transmission functions. The so-called virtual host, also known as "website space", is to divide a server running on the Internet into multiple "virtual" servers, each VM has an independent domain name and a complete Internet server (supporting WWW, FTP, E-mail, and so on). From the user's perspective, each virtual host is exactly the same as an independent server. In today's increasingly tight IP addresses, domain name-based virtual hosts are more widely used than IP-based virtual hosts.

Ii. System Environment

System Platform: RHEL 5.4

Nginx version: nginx-1.0.15

3. Configure an IP-based VM

Linux and FreeBSD operating systems allow you to add IP aliases. IP alias: You can bind multiple IP addresses to a physical network card. In this way, multiple IP-based virtual hosts can run on the same server with a single Nic. It is also very easy to set an IP alias. You only need to configure the network interface on the system so that it can listen to additional lP addresses. In Linux, you can use standard network configuration tools (such as ifconfig and route commands) to add IP aliases.

1) Use the ifconfig command to view the lP address of the server.

[Root @ linux nginx] # ifconfig
Eth0 Link encap: Ethernet HWaddr 08: 00: 27: 99: E4: 21
Inet addr: 10.0.0.20.bcast: 10.20.255.255 Mask: 255.0.0.0
Inet6 addr: fe80: a00: 27ff: fe99: e421/64 Scope: Link
Up broadcast running multicast mtu: 1500 Metric: 1
RX packets: 7324 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 2051 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 719461 (702.5 KiB) TX bytes: 308638 (301.4 KiB)


Lo Link encap: Local Loopback
Inet addr: 127.0.0.1 Mask: 255.0.0.0
Inet6 addr: 1/128 Scope: Host
Up loopback running mtu: 16436 Metric: 1
RX packets: 765 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 765 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 0
RX bytes: 48577 (47.4 KiB) TX bytes: 48577 (47.4 KiB)

[Root @ linux nginx] #
2) Add two lP aliases 10.0.0.189 and 10.0.0.190 to the eth0 Nic device. Run the ifconfig and route commands:

[Root @ linux nginx] # ifconfig eth0: 1 10.0.0.189 broadcast 10.20.255.255 netmask 255.0.0.0 up

[Root @ linux nginx] # route add-host 10.0.0.189 dev eth0: 1

[Root @ linux nginx] # ifconfig eth0: 2 10.0.0.190 broadcast 10.20.255.255 netmask 255.0.0.0 up

[Root @ linux nginx] # route add-host 10.0.0.190 dev eth0: 2

3) run the ifconfig command again to see that two lP aliases are bound to the eth0 Nic device.

[Root @ linux nginx] # ifconfig
Eth0 Link encap: Ethernet HWaddr 08: 00: 27: 99: E4: 21
Inet addr: 10.0.0.20.bcast: 10.20.255.255 Mask: 255.0.0.0
Inet6 addr: fe80: a00: 27ff: fe99: e421/64 Scope: Link
Up broadcast running multicast mtu: 1500 Metric: 1
RX packets: 7490 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 2151 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 FIG: 1000
RX bytes: 735277 (718.0 KiB) TX bytes: 342506 (334.4 KiB)

Eth0: 1 Link encap: Ethernet HWaddr 08: 00: 27: 99: E4: 21
Inet addr: 10.0.0.189 Bcast: 10.20.255.255 Mask: 255.0.0.0
Up broadcast running multicast mtu: 1500 Metric: 1

Eth0: 2 Link encap: Ethernet HWaddr 08: 00: 27: 99: E4: 21
Inet addr: 10.0.0.190 Bcast: 10.20.255.255 Mask: 255.0.0.0
Up broadcast running multicast mtu: 1500 Metric: 1

Lo Link encap: Local Loopback
Inet addr: 127.0.0.1 Mask: 255.0.0.0
Inet6 addr: 1/128 Scope: Host
Up loopback running mtu: 16436 Metric: 1
RX packets: 771 errors: 0 dropped: 0 overruns: 0 frame: 0
TX packets: 771 errors: 0 dropped: 0 overruns: 0 carrier: 0
Collisions: 0 txqueuelen: 0
RX bytes: 48897 (47.7 KiB) TX bytes: 48897 (47.7 KiB)

[Root @ linux nginx] #

4) Ping 10.0.0.189 and 10.0.0.190 from another server. If the IP address can be pinged, the configuration is correct. However, the IP alias configured through ifconfig and route disappears after the server is restarted. However, you can add these two ifconng and route commands to/etc/rc. the local file allows the system to run automatically when it is started. The following commands are used:
Vi/etc/rc. local
Add the following content at the end of the file and save it.

Ifconfig eth0: 1 10.0.0.189 broadcast 10.20.255.255 netmask 255.0.0.0 up

Route add-host 10.0.0.189 dev eth0: 1

Ifconfig eth0: 2 10.0.0.190 broadcast 10.20.255.255 netmask 255.0.0.0 up

Route add-host 10.0.0.190 dev eth0: 2

5) configure the IP-based virtual host in the Nginx configuration file (nginx. in conf), configure three pure static HTML-supported virtual hosts for the IP addresses 10.0.0.0.0, 10.0.0.189, and 10.0.0.190.

Http {
Include mime. types;
Default_type application/octet-stream;

Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';

# Access_log logs/access. log main;

Sendfile on;
# Tcp_nopush on;

Keepalive_timeout 65;

# Gzip on;
# First VM
Server {
Listen 10.0.0.20.: 80; # the IP address and port number of the listener.
Server_name 10.0.0.20.; # Host Name

Access_log logs/host1.access. log main; # access log file storage path

Location/
{
Root/usr/local/nginx/html/host1; # directory where HTML webpage files are stored
Index index.html index.htm; refer to the first page file from left to right. If you cannot find the index.html file, check the index.htm file as the homepage file.
}
}
# Second virtual host
Server {
Listen 10.0.0.189: 80;
Server_name 10.0.0.189;

Access_log logs/host2.access. log main;

Location/
{
Root/usr/local/nginx/html/host2;
Index index.html index.htm;
}
}
# Third VM
Server {
Listen 10.0.0.190: 80;
Server_name 10.0.0.190;

Access_log logs/host3.access. log main;

Location/
{
Root/usr/local/nginx/html/host3;
Index index.html index.htm;
}
}

From the configuration file above, we can see that a section of server {......} Is a virtual host. If you want to configure multiple virtual hosts, create multi-segment server {......} Configuration is convenient. You can also set the listening IP address and port to "listen 80" without writing an IP address, which indicates listening to port 80 of all IP addresses on the server, different virtual hosts can be distinguished by server_name.

Iv. Test

1) Restart nginx with a script.

[Root @ linux conf] # service nginx restart

2) create three directories host1, host2, and host3 under/usr/local/nginx/html/respectively. Separate an index.html file in the three directories and write your own IP addresses;

3) access the corresponding IP address through a browser.

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.