参考官方文档

什么是虚拟主机

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供 www 服务,这样就可以实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响的。

nginx 配置虚拟主机

1.在nginx目录中的html路径新增三个html文件 a.htnl b.html c.html

nginx 虚拟主机

2.在nginx.conf  修改server{} 复制三个server 分别为

1.端口80 ;域名www.gan.com

2.端口80 ;域名www.gan2.com

3.端口8081 ;域名www.gan2.com


    server {
        listen       80;
        server_name  www.gan.com;

        location / {
            root   html;
            index  a.html;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }

    server {
        listen       80;
        server_name  www.gan2.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  b.html;
        }
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }

    server {
        listen       8081;
        server_name  www.gan2.com;

        location / {
            root   html;
            index  c.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

修改完nginx.conf 后要测试一下,再重启 最后一个8081端口 还要在防火墙配置开放一下

[[email protected] nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] nginx]# nginx -s reload

[[email protected] nginx]# /sbin/iptables -I INPUT -p tcp --dport 8081 -j ACCEPT

3.修改本机host文件 来模拟 映射域名  我就加两条

192.168.1.105  www.gan.com
192.168.1.105  www.gan2.com

相关文章: