前戏

假设我们开发了连个网站,一个为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文件

相关文章:

  • 2021-11-13
  • 2021-09-19
  • 2021-07-03
  • 2021-08-13
  • 2021-06-22
  • 2021-05-11
  • 2021-09-09
  • 2021-10-07
猜你喜欢
  • 2021-04-20
  • 2021-09-02
  • 2021-12-26
相关资源
相似解决方案