nginx--虚拟主机

前戏

假设我们开发了连个网站,一个为www.zouzou.com,一个为www.balabala.com。如果每台服务器只能运行一个网站的话,那我们就需要买两台服务器,会造成资源的浪费。

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

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

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

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

基于域名的虚拟主机配置

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

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.准备不同的虚拟主机的资料

mkdir -p /data/{zouzou,balabala}

我的目录如下。两个index.html文件里写了不同的内容

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

4.更改本地的hosts文件

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

windows的hosts文件就在这里:C:WindowsSystem32driversetchosts ,写入如下信息

服务器的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你会看到如下信息

 各意义如下

原文地址:https://www.cnblogs.com/zouzou-busy/p/11622944.html