nginx 配置虚拟主机

1.为什么要建立虚拟主机呢?
     因为随着Internet用户的增多,越来越多的服务器开始承受不了巨大的访问量。这时呢,就出现了虚拟主机,虚拟主机可以使多台虚拟机共享一台真实主机的资源,大大的增强了放服务器和通讯线路的利用率,使得一台服务器上能够,毫无冲突地配置多个网络的ip地址,这意味着人们可以把多个域名建立在一个服务器上,不必再为建立一个站点而购置单独的服务器和用巨资申请专线作为信息的出入口。 现在大部分国外企业建站都采用这用服务硬盘空间租用的方式(即虚拟主机)。为适应我国进入WTO后日益激烈的国际商业竞争环境,加快对投资小、收益快的网上交易平台的应用,加强电子商务、企业上网等信息化建设的力度,虚拟主机建站已成为提高企业竞争力的重要手段。说白了, 虚拟主机就是在一台服务器启动多个网站。
      那么如何区分不同的网站呢:可以根据  域名不同 端口不同 来区别不同的网站。

2.通过端口区分不同虚拟机
     nginx的配置文件: /usr/local/nginx/conf/nginx.conf
#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节点就是一个虚拟主机
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
// Html是nginx安装目录下的html目录
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}
     可以配置多个server,配置了多个虚拟主机。
     添加虚拟主机:
#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  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-81;
            index  index.html index.htm;
        }
    }
}
     重新加载配置文件: [root@localhost nginx]# sbin/nginx -s reload

3.通过域名区分虚拟主机
(1) 什么是域名: 域名就是网站。
www.baidu.com
www.taobao.com
www.jd.com
Tcp/ip
        Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。
(2) 一级域名:
Baidu.com
Taobao.com
Jd.com
(2) 二级域名:
www.baidu.com
Image.baidu.com
(3) 三级域名:
aaa.image.baidu.com
说明:一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。
(4) 本地测试可以修改hosts文件。
     修改window的hosts文件:(C:\Windows\System32\drivers\etc)
     可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。

(5) Nginx的配置
#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  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html-81;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.taobao.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html-taobao;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.baidu.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html-baidu;
            index  index.html index.htm;
        }
    }
}
     域名的配置:
     192.168.25.148 www.taobao.com
     192.168.25.148 www.baidu.com



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员学习圈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值