Nginx基于IP,端口,域名配置虚拟主机

简介: Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。
+关注继续查看

Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。Nginx同Apache httpd一样,Nginx也提供基于IP,基于端口以及域名方式的形式来配置虚拟主机。

一、什么是虚拟主机

虚拟主机是使用特殊的软硬件技术,把一台真实的物理服务器主机分割成多个逻辑存储单元。每个逻辑单元都没有物理实体,但是每一个逻辑单元都能像真实的物理主机一样在网络上工作,具有单独的IP地址(或共享的IP地址)、独立的域名以及完整的Internet服务器(支持WWW、FTP、E-mail等)功能。

虚拟主机的关键技术在于,即使在同一台硬件、同一个操作系统上,运行着为多个用户打开的不同的服务器程式,也互不干扰。而各个用户拥有自己的一部分系统资源(IP地址、文档存储空间、内存、CPU等)。各个虚拟主机之间完全独立,在外界看来,每一台虚拟主机和一台单独的主机的表现完全相同。所以这种被虚拟化的逻辑主机被形象地称为“虚拟主机”。

二、基于端口的虚拟主机

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>

三、基于IP的虚拟主机

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>

四、基于域名的虚拟主机

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>
目录
相关文章
|
2月前
|
移动开发 应用服务中间件 nginx
统计请求nginx最多次数的IP地址
统计请求nginx最多次数的IP地址
|
3月前
|
应用服务中间件 Linux Shell
CentOS7下利用自带防火墙+Nginx封堵高频访问的恶意IP
CentOS7下利用自带防火墙+Nginx封堵高频访问的恶意IP
87 0
|
4月前
|
关系型数据库 MySQL Unix
nginx代理DB & ip限制
nginx代理DB & ip限制
|
4月前
|
应用服务中间件 nginx
Nginx配置访问IP白名单
Nginx配置访问IP白名单
422 1
|
5月前
|
应用服务中间件 nginx
nginx禁止未绑定域名或IP访问-全局设置
nginx禁止未绑定域名或IP访问-全局设置
65 0
|
7月前
|
JSON 网络协议 应用服务中间件
利用 Nginx 实现简易的公网IP查询
本文将介绍如何利用纯 Nginx 搭建简易的公网 IP 地址查询接口
102 0
|
10月前
|
网络协议 NoSQL 关系型数据库
【宝塔部署PHP项目】含域名访问部署、IP访问部署、数据库、端口号、Nginx等知识
【宝塔部署PHP项目】含域名访问部署、IP访问部署、数据库、端口号、Nginx等知识
1235 0
【宝塔部署PHP项目】含域名访问部署、IP访问部署、数据库、端口号、Nginx等知识
|
11月前
|
Linux 应用服务中间件 nginx
nginx系列-----虚拟主机(多IP地址)
虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响的。
159 0
nginx系列-----虚拟主机(多IP地址)
|
12月前
|
应用服务中间件 nginx CDN
nginx下套cdn获取真实用户IP
nginx下套cdn获取真实用户IP
254 0
nginx下套cdn获取真实用户IP
|
应用服务中间件 定位技术 网络安全
Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息
Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息
313 0
Nginx访问日志接入GrayLog4.2.5并通过GeoIP展现访问者IP的地理位置信息
相关产品
云迁移中心
推荐文章
更多