当前位置: 首页 > article >正文

nginx三种虚拟主机的配置(IP,端口,域名)

准备工作:

[root@bogon ~]# mkdir -p /data/nginx{1..3} #-p是用于递归创建使用
[root@bogon ~]# echo "hello nginx1" > /data/nginx1/index.html
[root@bogon ~]# echo "hello nginx2" > /data/nginx2/index.html
[root@bogon ~]# echo "hello nginx3" > /data/nginx3/index.html

一、基于ip地址不同配置虚拟主机

首先,添加IP地址并启动网卡

[root@bogon conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.238.141
[root@bogon conf.d]# nmcli connection modify ens33 +ipv4.addresses 192.168.238.151
[root@bogon conf.d]# nmcli connection up ens33

然后,在/data/nginx/conf.d/vhost.conf下配置(注意:要记得重新启动nginx)

server {
        listen 192.168.238.131:80;
        server_name localhost;
        location / {
                root /data/nginx1;
                index index.html;
        }
}

server {
        listen 192.168.238.141:80;
        server_name localhost;
        location / {
                root /data/nginx2;
                index index.html;
        }
}

server {
        listen 192.168.238.151:80;
        server_name localhost;
        location / {
                root /data/nginx3;
                index index.html;
        }
}

实现效果

二、基于端口不同配置虚拟主机 

在/data/nginx/conf.d/vhost.conf下配置

server {
        listen 80;
        server_name localhost;
        location / {
                root /data/nginx1;
                index index.html;
        }
}

server {
        listen 81;
        server_name localhost;
        location / {
                root /data/nginx2;
                index index.html;
        }
}

server {
        listen 82;
        server_name localhost;
        location / {
                root /data/nginx3;
                index index.html;
        }
}

实现效果

三、基于域名不同配置虚拟主机

server {
        listen 192.168.238.131:80;
        server_name www.nginx1.com;
        location / {
                root /data/nginx1;
                index index.html;
        }
}

server {
        listen 192.168.238.131:80;
        server_name www.nginx2.com;
        location / {
                root /data/nginx2;
                index index.html;
        }
}

server {
        listen 192.168.238.131:80;
        server_name www.nginx3.com;
        location / {
                root /data/nginx3;
                index index.html;
        }
}

然后,在/etc/hosts下添加IP和域名

[root@bogon ~]# vi /etc/hosts

192.168.238.131   www.nginx1.com
192.168.238.131   www.nginx2.com
192.168.238.131   www.nginx3.com

实现效果


http://www.kler.cn/a/158662.html

相关文章:

  • 绝对值线性化
  • AI常见的算法和例子
  • 爬虫基础(三)Session和Cookie讲解
  • 《大语言模型》综述学习笔记
  • 【开源免费】基于SpringBoot+Vue.JS公交线路查询系统(JAVA毕业设计)
  • 中国股市“慢牛”行情的实现路径与展望
  • 西南科技大学信号与系统A实验二(信号频谱分析)
  • Vue中 env 文件是如何读取的? 优先级?
  • springboot(ssm健身器材用品网 健身用品商城Java(codeLW)
  • 卷积神经网络训练情感分析
  • 基于ssm品牌会员在线商城源码
  • RepidJson将内容写入文件
  • 运维的职业成长路径是怎么样的?
  • DeepStream系列之rtmpsink功能,rtsp转rtmp,opencv读取rtsp图像处理后推流rtmp
  • Example: use raspberry pi 4 control multiple motors(tb660)
  • Doris 外部表
  • FIR IP 学习记录
  • 酷雷曼连续两届荣任北京软协理事会会员单位
  • 【Maven】清理 maven 仓库
  • C++ day55 判断子序列 不同的子序列
  • ssm的网上奶茶店系统(有报告)。Javaee项目。
  • UDP数据报套接字
  • 5G常用简称
  • MySQL索引优化及查询优化
  • rabbitmq消息队列实验
  • 基于SQL语言的数据库管理系统