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实现虚拟主机
[[email protected] ~]# 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 {
            listen       192.168.9.110:80;
            server_name  www.ocean.com ocean.com;       #基于主机名
            location / {
            root   html/ocean;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        }

    server {
            listen       192.168.9.120:80;
            server_name  www.blue.com blue.com;       #基于主机名
            location / {
            root   html/blue;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}



3. 检测配置文件并重新启动
[[email protected] ~]# /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


[[email protected] ~]# pgrep nginx
5856
5857
[[email protected] ~]# kill -HUP 5856
[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload


4.测试


Nginx之虚拟主机


Nginx之虚拟主机