nginx配置虚拟主机二(7.2.通过域名区分虚拟主机)

本地测试修改window的hosts文件:(C:\Windows\System32\drivers\etc)

可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走DNS服务器。

修改如下:

 nginx配置虚拟主机二(7.2.通过域名区分虚拟主机)

 修改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 {
        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       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;
        }
    }
	
	server {
        listen       80;
        server_name  www.sina.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-sina;
            index  index.html index.htm;
        }
    }
   
   
}

添加两个server:

nginx配置虚拟主机二(7.2.通过域名区分虚拟主机)

在nginx安装目录下分别创建html-baidu和html-sina目录

nginx配置虚拟主机二(7.2.通过域名区分虚拟主机)

重启nginx(在外部访问需要关闭防火墙)

nginx配置虚拟主机二(7.2.通过域名区分虚拟主机)