Nginx configures virtual hosts based on IP, port, and domain name

1. What is virtual host

A virtual host uses special software and hardware technology to divide a real physical server host into multiple logical storage units. Each logical unit has no physical entity, but each logical unit can work on the network like a real physical host, with a separate IP address (or shared IP address), an independent domain name, and a full Internet server (supporting WWW, FTP, E-mail, etc.) function.

The key technology of virtual host is that even on the same hardware and the same operating system, different server programs opened for multiple users are running without interfering with each other. Each user has its own part of the system resources (IP address, document storage space, memory, CPU, etc.). Each virtual host is completely independent, and from the outside, each virtual host behaves exactly the same as a single host. So this virtualized logical host is called "virtual host" vividly.

2. Port-based virtual host

1、准备环境
#当前环境
# more /etc/issue
Red Hat Enterprise Linux Server release 6.3 (Santiago)
Kernel \r on an \m

# uname -rm
2.6.32-279.el6.x86_64 x86_64

# nginx -v
nginx version: nginx/1.8.0

# 创建3个目录用于存放不同形式虚拟主机index.html文件
# mkdir -p /website/baseport
# mkdir -p /website/baseip
# mkdir -p /website/basedomain

# vi /website/baseport/index.html 
<!DOCTYPE html>
<html>
<head>
<title>Base port sample</title>
</head>
<body>
<h1>This is an based port website sample(prot:8080).</h1>
</body>
</html>

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

#第二个虚拟主机        
server {       
        listen       8080;
        server_name  localhost;

        location / {
            root   /website/port;
            index  index.html index.htm;
        }
    }

3、验证   
# nginx -t              #语法检查
# service nginx reload  #服务重载
# curl http://192.168.1.120:8080  #验证基于端口访问
<!DOCTYPE html>
<html>                                 
<head>
<title>Base port sample</title>
</head>
<body>
<h1>This is an based port website sample(prot:8080).</h1>
</body>
</html>

3. IP-based virtual hosting

1、先添加IP
# ifconfig|grep "inet addr"
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0
# ifconfig eth0:0 192.168.1.220 netmask 255.255.255.0 up  #添加IP到eth0:0
# ifconfig|grep "inet addr"
          inet addr:192.168.1.120  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:192.168.1.220  Bcast:192.168.1.255  Mask:255.255.255.0
          inet addr:127.0.0.1  Mask:255.0.0.0

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;

#第二个虚拟主机                      
 server {
        listen       192.168.1.220:80;
        server_name  localhost;

        location / {
            root   /website/baseip;
            index  index.html index.htm;
        }
    }

3、验证    
# nginx -t                     #语法检查      Author:Leshami                     
# service nginx reload         #服务重载      Blog  :http://blog.csdn.net/leshami
# curl http://192.168.1.220    #验证基于IP访问
<!DOCTYPE html>
<html>
<head>
<title>Base ip sample</title>
</head>
<body>
<h1>This is an based ip website sample.</h1>
</body>
</html>

4. Domain-based virtual hosting

1、修改/etc/hosts文件
# echo "
192.168.1.120 bbs.ycdata.net bbs
192.168.1.120 mail.ycdata.net mail
> ">>/etc/hosts

2、配置nginx.conf
#第一个虚拟主机
server {
        listen       80;
        server_name mail.ycdata.net;

        location / {
            root   html;
            index  index.html index.htm;
        }

#第二个虚拟主机        
server {
        listen       80;
        server_name  bbs.ycdata.net;

        location / {
            root   /website/baseport;
            index  index.html index.htm;
        }
    }

3、验证
# curl http://mail.ycdata.net
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

# curl http://bbs.ycdata.net
<!DOCTYPE html>
<html>
<head>
<title>Base port sample</title>
</head>
<body>
<h1>This is an based port website sample(prot:8080).</h1>
</body>
</html>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325070335&siteId=291194637