nginx--虚拟主机

前戏

假设咱们开发了连个网站,一个为www.zouzou.com,一个为www.balabala.com。若是每台服务器只能运行一个网站的话,那咱们就须要买两台服务器,会形成资源的浪费。php

虚拟主机就是将一台服务器分割成多个“虚拟服务器”,每一个站点使用各自的磁盘空间,这样。咱们就能够在一台服务器上来使用虚拟主机来部署网站。html

虚拟主机就是在web服务里的一个独立的网站站点,这个站点对呀独立的域名(IP),具备独立的程序和资源目录,能够独立的对外提供服务。这个独立的站点部署是在nginx.conf中使用server { } 代码块来表示一个虚拟主机,Nginx支持多个server { } 标签,既支持多个虚拟站点。nginx

nginx支持三种虚拟主机的类型web

  • 基于域名的虚拟主机:经过不一样的域名区分不一样的虚拟主机,是企业应用最广的虚拟主机。
  • 基于端口的虚拟主机:经过不一样的端口来区分不一样的虚拟主机,通常用做企业内部网站,不对外直接提供服务的后台
  • 基于IP的虚拟主机:经过不一样的IP区分不一样的虚拟主机,此类比较少见,通常业务须要多IP的常见都会在负载均衡中绑定VIP

基于域名的虚拟主机配置

1.修改nginx底下的conf/nginx.conf ,修改信息以下windows

server {
            listen       80;
            server_name  www.zouzou.com;
            location / {
                #指明网页根目录在/opt/html/文件夹下
                root   /data/zouzou;
                index  index.html index.htm;
            }
            }
        server {
            listen       80;
            server_name  www.balabala.com;
            location / {
                #指明网页根目录在/opt/html/文件夹下
                root   /data/balabala;
                index  index.html index.htm;
            }
            }

我将多余的数据删掉了浏览器

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    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"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.zouzou.com;
        location / {
            root   /data/zouzou;
            index  index.html index.htm;
        }
        location /status {
              stub_status on;
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}
server {
        listen       80;
        server_name  www.balabala.com;
        location / {
            root   /data/balabala;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}
}
我本身的nginx.conf文件

改成以后加载配置文件服务器

nginx -t   # 检测语法,在sbin下执行
nginx -s reload   # 平滑重启 ,在sbin下执行

3.准备不一样的虚拟主机的资料app

mkdir -p /data/{zouzou,balabala}

个人目录以下。两个index.html文件里写了不一样的内容负载均衡

[root@HH balabala]# tree /data
/data
├── balabala
│   └── index.html
└── zouzou
    └── index.html

4.更改本地的hosts文件tcp

由于咱们的两个网址www.zouzou.com和www.balabala.com是不存在的,写入到本地dns解析文件,因为我是在windows中经过浏览器访问,应该在windows的hosts文件中添加记录hosts文件就是一个本地dns(就是将域名转化成ip地址)强制解析的文件。

windows的hosts文件就在这里:C:\Windows\System32\drivers\etc\hosts ,写入以下信息

服务器的ip   www.zouzou.com
服务器的ip   www.balabala.com

配置好以后,浏览器就会优先使用本地的DNS解析。

 

nginx的状态模式

nginx与php-fpm同样内建了一个状态页,对于想了解nginx的状态以及监控nginx很是有帮助。为了后续的zabbix监控,咱们须要先了解一下nginx的状态页。

Nginx软件在编译时又一个with-http_stub_status_module模块,这个模块功能是记录Nginx的基本访问状态信息,让使用者了解Nginx的工做状态。

要想使用状态模块,在编译时必须增长--with-http_stub_status_module参数。

查看是否安装了status模块(我在编译安装的时候已经安装了)

[root@HH ~]# /opt/nginx1-16/sbin//nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt/nginx1-16/ --with-http_ssl_module --with-http_stub_status_module
[root@HH ~]#
--with-http_stub_status_module 就是status模块

1.在配置文件中,添加一个参数便可

location /status {
     stub_status on;
}

  加载配置文件

nginx -t   # 检测语法,在sbin下执行
nginx -s reload   # 平滑重启 ,在sbin下执行

访问:www.zouzou.com/status你会看到以下信息

 各意义以下

相关文章
相关标签/搜索