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 @   知己一语  阅读(99)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示