美文网首页
nginx 三种虚拟主机

nginx 三种虚拟主机

作者: 心疼你萌萌哒 | 来源:发表于2018-05-30 20:26 被阅读0次
vim /etc/hosts
127.0.0.1 web.yahoo.com
虚拟主机
基于域名的虚拟主机:
    server {
        listen        80;
        server_name  web.yahoo.com;
        location /{
            root /yahoo/html;
            index index.html index.htm;
                }
        }
    server {
        listen        80;
        server_name  web.wing.com;
        location /{
            root /wing/html;
            index index.html index.htm;
                }
        }
    server {
    listen 80 default;
    location /{
                root /default/html;
                index index.html index.htm;
                    }
    }
如果浏览器直接通过IP地址或者其他指向这台机器的域名访问, 那么访问到的是第三个server配置。第三个server为一个默认配置, 请注意它没有“server_name”指令, 并且“listen”指令包含一个“default”关键字。
注:默认如果没有按域名访问虚拟主机也没有指定默认虚拟主机则按顺序访问

基于端口的虚拟主机:
    server {
        listen       1050;
        server_name  web.105.com;
        location /{
            root /105/html;
            index index.html index.htm;
                }
        }
    server {
        listen       1060;
        server_name  web.106.com;
        location /{
            root /106/html;
            index index.html index.htm;
                }
        }
         [root@localhost conf]#vim /etc/hosts
         [root@localhost conf]#127.0.0.1 web.105.com
         [root@localhost conf]#echo 105 > /105/html/index.htm
         [root@localhost conf]# curl web.105.com:1050
        105

        
基于IP的虚拟主机:要和真机同网段
    server {
        listen      10.18.44.105:80;要和真机同网段
        server_name  web.105.com;
        location / {
            root   /105/html; 
            index  index.html index.htm;
        }
    server {
        listen      10.18.44.106:80;
        server_name  web.106.com;
        location /{
            root /106/html;
            index index.html index.htm;
                }
        }
       
  vim /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
10.18.44.105 web.105.com
 [root@localhost conf]#echo 105 > /105/html/index.html

 [root@localhost conf]# curl 10.18.44.105:80
     105

相关文章

网友评论

      本文标题:nginx 三种虚拟主机

      本文链接:https://www.haomeiwen.com/subject/gxixsftx.html