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

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>
时间: 2024-08-30 22:53:10

Nginx基于IP,端口,域名配置虚拟主机的相关文章

Nginx中如何配置虚拟主机

虚拟主机:将一台服务器虚拟出多台主机,每台虚拟主机都可以是一个独立的网站,都可以具有独立的域名,具有完整的Intemet服务器功能.同一台主机上的虚拟主机之间是完全独立的. 简单说就是你有两个完全独立的网站,可以利用虚拟主机在一台服务器上跑. 跟Apache -样,Nginx也可以配置多种类型的虚拟圭机:一是基于IP的虚拟主机,二是基于域名的虚拟主机,三是基于端口的虚拟主机. nginx.conf http { --- #server就是定义主机的 #server { # listen 8000

如何编写安装配置httpd服务基于域名的虚拟主机脚本

编写安装配置httpd服务基于域名的虚拟主机脚本 要求: 1.自动完成安装,基本配置 2.通过提示输入域名.网站根目录.等信息完成虚拟主机的添加工作 #!/bin/bash service httpd stop &> /dev/null conf="/usr/local/apache/conf/httpd.conf" vhostconf="/usr/local/apache/conf/extra/httpd-vhosts.conf" bin="

apache服务器一个ip(如:127.0.0.1)和多个域名(虚拟主机)的绑定_Linux

今天在学习PHP时,有这样的一个需求:一个ip(如:127.0.0.1)和多个域名(虚拟主机)绑定,以下是我的解决方案: 解决方案一:通过端口来区分不同的虚拟主机 ①按照绑定一个站点的方法做好准备 1. 先开发好自己的网站(d:/myblog(存放在D盘的myblog目录下)) 2. 配置httpd.conf文件(存放在apache安装目录的conf文件夹中),启用httpd-vhosts.conf(把第二行前面的#号去掉即可). 3. 配置httpd-vhosts.conf文件(存放在apac

弄了两天了!! Ubutun 下nginx配置虚拟主机问题

问题描述 弄了两天了!! Ubutun 下nginx配置虚拟主机问题 1.下两图是sites-available/default 的配置 2.执行nginx-t命令后出现的错误 3.这是配置的虚拟主机根目录 现在问题来了,在浏览器中输入localhost可以正常显示nginx欢迎界面,但输入localhost/info.php无法显示页面,有没有哪位大神给我指点一二,感激不尽!!! 解决方案 80端口侦听需要root权限,所以需要sudo执行nginx进程. 其次要看一下你的conf配置文件好像

Linux下Nginx配置虚拟主机VirtualHost实例教程

增加 Nginx 虚拟主机 这里假设大家的 Nginx 服务器已经安装好, 不懂的请阅读各 linux 发行版的官方文档或者 LNMP 的安装说明. 配置 Virtual host 步骤如下: 进入 /usr/local/nginx/conf/vhost 目录, 创建虚拟主机配置文件 demo.neoease.com.conf ({域名}.conf). 打开配置文件, 添加服务如下:    代码如下 复制代码 server {     listen          80;           

Tomcat的设置4——Tomcat的体系结构与设置基于端口号的虚拟主机

一.Tomcat体系结构 从conf/server.xml可体现Tomcat的体系.一个Server可有多个service,一个service可以有多个连接器connector,每个连接器暴露出不同的端口进行不同的操作.比如图示的JK连接器程序通过一个connector进行集群处理,不需要集群的时候可以将该connector在配置文件中删除,做到了可插拔式.该图参考张孝祥老师tomcat视频. 二.基于端口号的虚拟主机 一个server可以有多个service,新增service2: <Serv

CentOS配置虚拟主机virtualhost使服务器支持多网站多域名的方法_服务器其它

本文实例讲述了CentOS配置虚拟主机virtualhost使服务器支持多网站多域名的方法.分享给大家供大家参考,具体如下: 如何让centos(redhat)配置虚拟主机,让服务器支持多个网站,针对Apache,只需要你修改apache配置文件/etc/httpd/conf/httpd.conf即可. 里面有个example文件,你只要对应配置即可. #<VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com # Do

分享三种Apache配置虚拟主机的方式_Linux

一.基于IP  1. 假设服务器有个IP地址为192.168.1.10,使用ifconfig在同一个网络接口eth0上绑定3个IP: [root@localhost root]# ifconfig eth0:1 192.168.1.11 [root@localhost root]# ifconfig eth0:2 192.168.1.12 [root@localhost root]# ifconfig eth0:3 192.168.1.13  2. 修改hosts文件,添加三个域名与之一一对应:

Apache中配置虚拟主机具体说明

在这里,我只介绍一种简单也比较常用的配置虚拟主机的方法.就是基于名称的虚拟主机配置: 第一步: 在conf文件中的httpd.conf中找到  代码如下 复制代码 # Virtual hosts #Include conf/extra/httpd-vhosts.conf 然后把上面的改为: # Virtual hosts Include conf/extra/httpd-vhosts.conf 第二步: 在conf/extra文件下找到httpd-vhosts.conf,在最后面添加如下代码: