Nginx虚拟主机

配置nginx 虚拟主机

一 创建基于域名的虚拟主机

1.1 为虚拟主机提供域名解析

可以使用dns 或者在 /etc/hosts中配置

echo "192.168.23.103 www.mynet.com   www.benet.com"  >> /etc/hosts   

1.2 为虚拟主机准备网页文档

mkdir -p /var/www/html/mynet
mkdir -p /var/www/html/benet
echo "this is mynet.com" > /var/www/html/mynet/index.html
echo "this is benet.com" > /var/www/html/benet/index.html


1.3 修改nginx 配置文件

vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  www.mynet.com;   #设置域名 www.mynet.com
        charset utf-8;                #设置网页字符集
        access_log  logs/www.mynet.access.log ;  #设置www.mynet.com网站的访问日志
        location / {
            root   /var/www/html/mynet;   #设置网页文件根目录
            index  index.html index.htm;   #设置首页文件
        }
        ....
    }
    
    
    server {
        listen       80; 
        server_name  www.benet.com;   #设置域名为 www.benet.com
         
        charset utf-8;

        access_log  logs/www.benet.access.log  ;#设置www.benet.com 访问日志

        location / { 
            root   /var/www/html/benet;   #设置网页文件根目录
            index  index.html index.htm;
        ........
        }  
     }
    

1.4 重载配置,并测试

 nginx  -t
 nginx  -s reload
 
 curl  http://www.mynet.com
 curl  http://www.benet.com

image-20210814202703455


二: 配置基于ip 的虚拟主机

2.1 添加网卡,或者配置虚拟ip

ifconfig ens33:0 192.168.23.130 netmask 255.255.255.0

image-20210814202848336


2.2 修改配置文件

vim /usr/local/nginx/conf/nginx.conf
 server {
        listen       192.168.23.130:80;  #设置监听地址为 192.168.23.130:80
        server_name  localhost;

        charset utf-8;

        location / { 
            root  html; 
            index  index.html index.htm;
        }
  }
  
   server {
        listen       192.168.23.103:80;  #设置监听地址为 192.168.23.103:80
        server_name  localhost;

        charset utf-8;

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

2.3 重载配置并测试

echo "hello" > /usr/local/nginx/html/index.html 
nginx -t
nginx -s reload

netstat -natp | grep :80

image-20210814204302670


curl http://192.168.23.103

curl http://192.168.23.130

image-20210814204133839


三: 基于端口的虚拟主机

3.1 修改配置文件

 vim /usr/local/nginx/conf/nginx.conf
  server {
        listen       192.168.23.103:80;  #设置监听地址为 192.168.23.103:80
        server_name  localhost;

        charset utf-8;

        location / { 
            root  html; 
            index  index.html index.htm;
        }
  }
  
   server {
        listen       192.168.23.103:80;  #设置监听地址为 192.168.23.103:8080
        server_name  localhost;

        charset utf-8;

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

3.2 重载配置并测试

nginx -t
nginx -s reload

netstat -natp | grep nginx

image-20210814205423073


curl http://192.168.23.103:80

curl http://192.168.23.103:8080

image-20210814205502931

posted @ 2021-08-15 06:08  知己一语  阅读(90)  评论(0编辑  收藏  举报