基于Nginx的虚拟主机的实现

1.1、 介绍:

  • Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
  • 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
  • 在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

1.2、优点

  • Nginx 可以在大多数 UnixLinux OS 上编译运行,并有 Windows 移植版。 Nginx 的1.4.0稳定版已经于2013年4月24日发布,一般情况下,对于新建站点,建议使用最新稳定版作为生产版本,已有站点的升级急迫性不高。Nginx 的源代码使用 2-clause BSD-like license。
  • Nginx 是一个很强大的高性能Web和反向代理服务,它具有很多非常优越的特性:
  • 在连接高并发的情况下,Nginx是Apache服务不错的替代品:Nginx在美国是做虚拟主机生意的老板们经常选择的软件平台之一。能够支持高达 50,000 个并发连接数的响应,感谢Nginx为我们选择了 epoll and kqueue作为开发模型。

1.3、nginx服务器

  • Nginx作为负载均衡服务:Nginx 既可以在内部直接支持 Rails 和 PHP 程序对外进行服务,也可以支持作为 HTTP代理服务对外进行服务。Nginx采用C进行编写,不论是系统资源开销还是CPU使用效率都比 Perlbal 要好很多。
  • 处理静态文件,索引文件以及自动索引;打开文件描述符缓冲。
  • 无缓存的反向代理加速,简单的负载均衡和容错。
  • FastCGI,简单的负载均衡和容错。
  • 模块化的结构。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理服务器处理单页中存在的多个 SSI,则这项处理可以并行运行,而不需要相互等待。
  • 支持 SSL 和 TLSSNI。

1.4、nginx代码

  • Nginx代码完全用C语言从头写成,已经移植到许多体系结构和操作系统,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函数库,并且除了zlib、PCRE和OpenSSL之外,标准模块只使用系统C库函数。而且,如果不需要或者考虑到潜在的授权冲突,可以不使用这些第三方库。

1.5、代理服务器

  • 作为邮件代理服务:Nginx 同时也是一个非常优秀的邮件代理服务(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。
  • Nginx 是一个安装非常的简单、配置文件非常简洁(还能够支持perl语法)、Bug非常少的服务。Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够不间断服务的情况下进行软件版本的升级。
    接下来开始nginx的环境部署

接下来进行nginx的安装

nginx安装包链接如下链接:https://pan.baidu.com/s/1iAI0ZVY13lvdMSgtdiAU5Q
提取码:1prj

  • 主机分配如下
服务 IP
nginx 192.168.20.30
2、nginx的安装命令
[root@lpj3 ~]# useradd -M -s /sbin/nologin nginx
[root@lpj3 ~]#  yum -y install gcc gcc-c++ openssl-devel zlib-devel pcre-devel
[root@lpj3 ~]# tar -zxf nginx-1.11.5.tar.gz 
[root@lpj3 ~]# cd nginx-1.11.5
[root@lpj3 nginx-1.11.5]#  ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module 
[root@lpj3 nginx-1.11.5]# make && make install
[root@lpj3 nginx-1.11.5]#  ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
查看当前nginx的版本
[root@lpj3 nginx-1.11.5]#   nginx -v
nginx version: nginx/1.11.5

nginx的常用命令如下

检测配置文件语法: nginx -t
启动:  nginx
关闭:  nginx -s stop
重新加载: nginx -s reload
重启:先停止 再开启

启动成功后 访问本机IP 则访问到如下页面 证明nginx安装成功
访问本机IP:http://192.168.20.30/
在这里插入图片描述
如果访问本机IP不成功的话放行端口进行操作

[root@lpj3 nginx-1.11.5]# firewall-cmd --zone=public --add-port=80/tcp --permanent  ##添加端口80
success
[root@lpj3 nginx-1.11.5]# firewall-cmd --reload  刷新
success

3.、nginx中配置文件的层次

在这里插入图片描述

events {
 events里的配置项
}

http {
    server {
        location / {
        }
    }
}

nginx中各模块的含义

- main 用于nginx全局信息的配置
- events nginx工作模式的配置(nginx支持的模型有select poll epoll - - kqueue(BSD系统) kqueue效率很高 类似于epoll)
- http http协议信息的配置
- upstream 负载均衡模块(默认没有 需要自己手写 与server是平行模块)
- server 服务器访问信息配置
- location 用于进行访问的路由 location会带有独特的匹配表示 用于匹配不同的请求

4、 基于nginx的虚拟主机的实现

  • 虚拟主机定义:
  • 在web服务中就是一个独立的网站站点,这个独立的站点可能会有独立的域名、IP、端口,具有独立的程序和资源目录,可以独立的对外提供访问服务
    下面进行操作
  • 基于域名的虚拟主机
[root@lpj3 nginx-1.11.5]# vim /usr/local/nginx/conf/nginx.conf
 在原有的server下在添加一个server 内容如下
 server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
          }
  server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

更改后内容如下:
   server {
        listen       80;
        server_name  www.xiyangyang.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/nginx/html/meiyangyang;
            index  index.html index.htm;
        }
     }
     server {
        listen       80;
        server_name  nuanyangyang;

        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /usr/local/nginx/html/feiyangyang;
            index  index.html index.htm;
        }
创建相对应的目录并做域名解析
[root@lpj3 nginx-1.11.5]# mkdir /usr/local/nginx/html/{meiyangyang,feiyangyang}
[root@lpj3 nginx-1.11.5]# echo "喜羊羊和灰太狼" > /usr/local/nginx/html/meiyangyang/index.html
[root@lpj3 nginx-1.11.5]# echo "狼太灰和洋洋喜" > /usr/local/nginx/html/meiyangyang/index.html
[root@lpj3 nginx-1.11.5]# vim /etc/hosts
192.168.20.30 www.xiyangyang.com
192.168.20.30 www.nuanyangyang.com

检测语法并重新加载nginx

[root@lpj3 nginx-1.11.5]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lpj3 nginx-1.11.5]# nginx -s reload
进行验证 
- curl http://www.xiyangyang.com
- curl http://www.nuanyangyang.com
- [root@lpj3 nginx-1.11.5]# curl http://www.xiyangyang.com
- 喜羊羊和灰太狼
- [root@lpj3 nginx-1.11.5]# curl http://www.nuanyangyang.com
- 狼太灰和洋洋喜
基于IP的虚拟主机
- 添加一个IP用于测试
[root@lpj3 ~]# ifconfig ens33:2 192.168.20.170/24
[root@lpj3 ~]# ifconfig | grep 170
        inet 192.168.20.170  netmask 255.255.255.0  broadcast         192.168.20.255
修改配置文件
server {
        listen       80;
        server_name  192.168.20.170;#域名更改为IP

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/nginx/html/meiyangyang;
            index  index.html index.htm;
        }
     }
     server {
        listen       80;
        server_name  192.168.20.30; #把域名更改为IP

        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /usr/local/nginx/html/feiyangyang;
            index  index.html index.htm;
        }
检测语法并重新加载nginx
[root@lpj3 nginx-1.11.5]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lpj3 nginx-1.11.5]# nginx -s reload   
进行验证
[root@lpj3 ~]# curl http://192.168.20.30
狼太灰和洋洋喜
[root@lpj3 ~]# curl http://192.168.20.170
喜羊羊和灰太狼
基于不同端口的虚拟主机的实现
更改配置文件
server {
        listen       80;#这个端口保持不变
        server_name  www.xiyangyang.com;IP更改为域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /usr/local/nginx/html/meiyangyang;
            index  index.html index.htm;
        }
     }
     server {
        listen       9999;#默认端口为80,随机更改  更改端口数字最好大于9000
        server_name  www.nuanyangyang.com; #把IP更改为域名

        #charset koi8-r;
        
        #access_log  logs/host.access.log  main;
        
        location / {
            root   /usr/local/nginx/html/feiyangyang;
            index  index.html index.htm;
        }
检测语法并重新加载nginx
[root@lpj3 nginx-1.11.5]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
进行验证
[root@lpj3 ~]# curl http://192.168.20.30
喜羊羊和灰太狼
[root@lpj3 ~]# curl http://192.168.20.30:9999
狼太灰和洋洋喜

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章