天天看點

Nginx之虛拟主機

Nginx 虛拟主機

========================================================

虛拟主機是在網絡伺服器上劃分出一定的磁盤空間供使用者放置站點、應用元件等,提供必要的站點功能、資料存放和傳輸功能。所謂虛拟主機,也叫“網站空間”,就是把一台運作在網際網路上的伺服器劃分成多個“虛拟”的伺服器,每一個虛拟主機都具有獨立的域名和完整的Internet伺服器(支援WWW、FTP、E-mail等)功能,從使用者角度來看,每台虛拟術機和一台獨立的伺服器完全相同,在IP位址日益緊張的今天,基于域名的虛拟主機要比基于IP的虛拟主機使用的更加廣泛。

三種表現形式:

基于主機

基于端口

基于IP

準備工作:

安裝好nginx,這裡不做介紹,如果需要請在本博中查找。

一、規劃

網站                 IP                   網站主目錄                    

www.jeffery.com      192.168.9.110       /nginx/jeffery

www.ocean.com        192.168.9.110       /nginx/ocean    

www.blue.com         192.168.10.120      /nginx/blue

由于使用了兩個IP,在接口上綁定IP

Nginx之虛拟主機

二、DNS解析

這裡可以直接寫入到 hosts 檔案

www.jeffery.com      jeffery.com        ==>    192.168.9.110

www.ocean.com        ocean.com          ==>    192.168.9.110

www.blue.com         blue.com           ==>    192.168.9.120

Nginx之虛拟主機

三、Nginx虛拟主機

1. 準備工作

Nginx之虛拟主機

2.配置Nginx實作虛拟主機

[root@jeffery ~]# vim /usr/local/nginx/conf/nginx.conf

#工作模式及連接配接數上限

worker_processes  1;               #cpu核心數

events {

    worker_connections  1024;      #連接配接數

}

#設定http伺服器,利用它的反向代理功能提供負載均衡支援

http {

    include            mime.types;

    default_type       application/octet-stream;

    sendfile           on;

    keepalive_timeout  65;

    server {

            listen       192.168.9.110:80;

            server_name  www.jeffery.com jeffery.com;       #基于主機名

            location / {               

            root   html/jeffery;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

            server_name  www.ocean.com ocean.com;       #基于主機名

            location / {

            root   html/ocean;

        }

            listen       192.168.9.120:80;

            server_name  www.blue.com blue.com;       #基于主機名

            root   html/blue;

    }

3. 檢測配置檔案并重新啟動

[root@jeffery ~]# /usr/local/nginx/sbin/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@Jeffery ~]# pgrep nginx

5856

5857

[root@Jeffery ~]# kill -HUP 5856

[root@Jeffery ~]# /usr/local/nginx/sbin/nginx -s reload

4.測試

Nginx之虛拟主機

Nginx之虛拟主機

繼續閱讀