2.nginx--配置虚拟主机

版权声明:转载请标明出处~~ https://blog.csdn.net/weixin_43231076/article/details/83096184

一、通过端口号区分虚拟主机:

1、进入/usr/local/nginx/confz 目录中,vim nginx.conf 打开这个文件,能看到里面配置了一个server节点,一起server节点就是一个虚拟主机

 如:server {										---一个server节点就是一个虚拟主机
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;					--这个nginx安装目录下的html文件夹
        index  index.html index.htm;
    }
 }

2、使用notepad++的sftp插件,连接nginx服务器,修改nginx.conf文件(因为要修改的内容较多,在window下修改方便一点)

 怎么安装和使用sftp插件见百度

3、在nginx.conf文件中配置一个新的server节点,通过端口区分虚拟主机

  server {
    listen       81;				--这里使用81端口
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html81;				--指向html81目录,所以要在同级目录下复制一份html81文件夹
        index  index.html index.htm;
    }
}

4、启动nginx后,访问81端口,就能访问html81文件夹下面的index.html

二、通过域名区分虚拟主机:

1、知识点:一个域名只对应一个ip地址,我们访问别的网站,其实都是通过ip地址来访问的。

       当我们在浏览器输入网站的域名时,电脑的DNS服务器会把域名解析成ip地址来访问。
	   但是,一个ip地址可以绑定多个域名
如果在本机的hosts文件中配置了ip地址和域名的对应关系,则访问域名就不会通过DNS服务器去解析域名,会直接访问
在host文件中绑定的ip

2、在nginx.conf新配置两个server节点,让他们的端口号都是80,但是server_name不一样,通过域名的不同,访问不同的网站

 server {
    listen       80;
    server_name  www.test.com;		--域名不同

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html-test;			--访问的文件夹资源不同
        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;
    }
}
 然后在自己本机的hosts文件中分别配置 www.test.com、www.test1.com都绑定服务器的ip,就能通过这两个域名访问服务器中不同的资源

猜你喜欢

转载自blog.csdn.net/weixin_43231076/article/details/83096184