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你會看到以下信息

 各意義以下

相關文章
相關標籤/搜索