nginx通过域名区分不同虚拟机<3>

什么是域名
域名就是网站。

www.baidu.com
www.taobao.com
www.jd.com

Tcp/ip

Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。

一级域名:
Baidu.com
Taobao.com
Jd.com

二级域名:
www.baidu.com
Image.baidu.com
Item.baidu.com

三级域名:

Image.baidu.com
Aaa.image.baidu.com

一个域名对应一个地址,一个ip地址可以被多个域名绑定。本地测试可以修改文件。

修改window的hosts文件:C:\Windows\System32\drivers\etc\host
打开后直接在后面加ip:域名键值对即可。

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


image.png
  1. 在C:\Windows\System32\drivers\etc\hosts中添加

192.168.231.129 www.test1.com
192.168.231.129 www.test2.com

  1. 在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       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.test1.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-test1;
            index  index.html index.htm;
        }
    }
      server {
        listen       80;
        server_name  www.test2.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

3.分别复制一份html文件,命名为html-test1,html-test2。修改html/index.html中的标题,用来区别不同的网站

  1. nginx重新加载配置文件

sbin/nginx -s reload

5.在浏览器中分别输入
www.test1.com
www.test2.com

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,090评论 0 9
  • 阅读之前,建议先阅读初识 Nginx。 之后,我们来了解一下 Nginx 配置。 抽象来说,将 Nginx 配置为...
    思梦PHP阅读 1,366评论 2 10
  • 第一章 Nginx简介 Nginx是什么 没有听过Nginx?那么一定听过它的“同行”Apache吧!Ngi...
    JokerW阅读 32,803评论 24 1,002
  • 在React中,常用Redux来做一些业务逻辑处理。Redux提供了中间件的写法,可以对Redux中的数据流做一些...
    VictorLiang阅读 713评论 0 1
  • 年轻人最迷茫的就是不知道自己这一生应该做些什么。是的,我就是这些年轻人其中的一个,我很迷茫,非常迷茫。 有些人很幸...
    深水里的星星阅读 278评论 1 1