Nginx实现多虚拟主机配置

一、Nginx的应用概述

Nginx作为一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。主要有以下3方面的应用:

1、http服务器

Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

2、虚拟主机

可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

3、反向代理,负载均衡

当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。 

 

二、什么是虚拟主机
      虚拟主机使用的是特殊的软硬件技术,它把一台服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以具有独立的域名,具有完整的Intemet服务器功能(WWW、FTP、Email等),同一台主机上的虚拟主机之间是完全独立的。从网站访问者来看,每一台虚拟主机和一台独立的主机完全一样。
       利用虚拟主机,不必为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程。虚拟主机提供了在同一台服务器、同一组Nginx进程上运行多个网站的功能。

可以实现在同一台服务器上运行多个网站,并且网站之间相互独立各不干扰。

 

三、基于IP 的虚拟主机配置

1、如果是多网卡就可以直接使用不同的IP地址,如果是单网卡则再系统中虚拟出两个网卡,设置为不同的IP地址

将/etc/sysconfig/network-scripts/ifcfg-eth0文件复制一份,命名为ifcfg-eth0:1

cd /etc/sysconfig/network-scripts

cp ifcfg-eth0 ifcfg-eth0:1

修改其中内容:

DEVICE=eth0:1

IPADDR=192.168.72.49

其他项不用修改,然后执行service network restart 重启网络服务

  1. 修改nginx配置文件

    在nginx/conf/nginx.conf是nginx核心配置文件,nginx对虚拟主机的配置,一个server就是一个虚拟主机。Nginx对于多虚拟主机的支持,主要是对server标签的添加,指定location启动路径即可。

    1)先将/nginx/html文件复制成两份,修改html/index.html文件,用于标记不同nginx首页信息。

    2)修改nginx.conf配置文件,添加两个server节点,指定ip

worker_processes  4;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}

http {
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       192.168.2.158:80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    server {
        listen       192.168.2.159:80;
        server_name  localhost;
        location / {
            root   test;
            index  index.html ;
        }
  }
}

  

四、基于不同端口的虚拟主机配置

在nginx conf 的配置目录下添加,port2.conf ,然后在nginx.conf 里面 include port2.conf

nginx.conf:

worker_processes  4;
pid        logs/nginx.pid;
events {
    worker_connections  1024;
}


http {
    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"';

    sendfile        on;
    keepalive_timeout  65;
	include  port2.conf;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

port2.conf, 在nginx 根目录添加test目录作为第二个server的root目录,里面新加修改的index.html

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        error_page  404              /baselogin.PNG;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

nginx -s reload

 

 

五:基于不同域名的虚拟主机:

为了测试,我们在hosts里面新加了两个 IP 域名解析

192.168.2.158 1.imoocc.com
192.168.2.158 2.imoocc.com

这样直接将nginx.conf 里面的hostname 修改为 1.imoocc.com  port2.conf 里面的hostname 修改为 2.imoocc.com,,然后 nginx -s reload

结果如下:

 

 

posted @ 2020-03-10 22:57  sucre_tan  阅读(913)  评论(0编辑  收藏  举报