Linux-Nginx虚拟主机

文章目录

🏡作者主页:点击!

🤖Linux专栏:点击!

⏰️创作时间:2024年11月21日9点32分

虚拟主机

配置不同域名不同端口号访问到不同内容

bash 复制代码
mkdir /data/nginx{1..3}    #创建三个目录
echo "hello nginx-1" > /data/nginx1/index.html
echo "hello nginx-2" > /data/nginx2/index.html
echo "hello nginx-3" > /data/nginx3/index.html
cd /etc/nginx/conf.d/
mv static.conf static.conf.bak

vi /etc/nginx/conf.d/vhost.conf

	server{
	listen 81;    #监听81端口
	server_name localhost;    #域名为www.test.com
	location / {    # / 意思是随意匹配
	root /data/nginx1;    #主目录为 Nginx1
	index index.html;    #默认页面为index.html
	}
	}

	server{
	listen 82;    #监听82端口
	server_name localhost;    #域名为www.test.com
	location / {    # / 意思是随意匹配
	root /data/nginx2;    #主目录为 Nginx2
	index index.html;    #默认页面为index.html
	}
	}

	server{
	listen 83;    #监听83端口
	server_name localhost;    #域名为www.test.com
	location / {    # / 意思是随意匹配
	root /data/nginx3;    #主目录为 Nginx3
	index index.html;    #默认页面为index.html
	}
	}

nginx -t    #检查 Nginx 服务
nginx -s reload    #重新加载服务

curl localhost:81    #使用命令验证
curl localhost:82
curl localhost:83

路由规则

bash 复制代码
cd /data/
mv nginx1 Nginx1    #将 nginx1 改为 Nginx1

vi /etc/nginx/conf.d/vhost.conf    #修改虚拟主机的配置文件

	server{
	listen 81;    #监听81端口
	server_name localhost;    #域名为www.test.com
	location / {    # / 意思是随意匹配
	root /data/nginx1;    #主目录为 /data/nginx1
	index index.html;    #默认页面为index.html
	}
	}

	server{
	listen 82;    #监听82端口
	server_name localhost;    #域名为www.test.com
	location /nginx2 {    # 所有以 nginx2 开头的请求都会被重定向到 /data 目录
	root /data;    #主目录为 /data
	index index.html;    #默认页面为index.html
	}
	}

#第三个nginx不做修改,依旧是默认匹配
nginx -s reload    #保存并重新加载

curl localhost:81    #此处提示失败,因为本地的路径已经改为 Nginx1,配置文件里面是 nginx1
curl localhost:82    #此处返回的是Nginx on openeuler 最早的界面,在data下面没有index.html文件
curl localhost:82/nginx2    #加上nginx2参数之后,nginx2是个目录需要重定向
curl localhost:82/nginx2/    #此时就能正常访问到了对应的index.html文件
curl localhost:83    #此处显示成功
bash 复制代码
vi /etc/nginx/conf.d/vhost.conf
	server{
	listen 81;    #监听81端口
	server_name localhost;    #域名为www.test.com
	location ~* \.html$ {    #不分大小写的来匹配 .html 后缀的文件
	root /data/Nginx1;    #主目录为
	index index.html;    #默认页面为index.html
	}
	}	

	server{
	listen 82;    #监听82端口
	server_name localhost;    #域名为www.test.com
	location = /nginx2/index.html {    #意思是精准匹配/nginx2/index.html
	root /data;    #主目录为/data
	index index.html;    #默认页面为index.html
	}
	}

	server{
	listen 83;    #监听83端口
	server_name localhost;    #域名为www.test.com
	location = / {    #精准匹配 / 接受/的请求,并重定向到 /data/nginx3
	root /data/nginx3;    #主目录为
	index index.html;    #默认页面为index.html
	}
	}

nginx -s reload    #重新加载服务

curl localhost:81    #会在/data/Nginx1目录下寻找所有匹配的html文件并返回
curl localhost:82/nginx2/index.html    #需要严格对应/nginx2/index.html才能做到正常回显
curl localhost:83    #接受 /,我们文件是不可能 / 结尾的,因此返回的是默认页面
bash 复制代码
vi /etc/nginx/conf.d/vhost.conf
	server{
	listen 81;    #监听81端口
	server_name localhost;    #域名为www.test.com
	location /Nginx1 {    #不分大小写的来匹配 .html 后缀的文件
	root /data;    #主目录为
	index index.html;    #默认页面为index.html
	}
	}	

	server{
	listen 82;    #监听82端口
	server_name localhost;    #域名为www.test.com
	location /nginx2 {    #意思是精准匹配/nginx2/index.html
	alias /data/nginx2/index.html;    #别名为/data,此时alias为绝对路径,root为相对路径
	index index.html;    #默认页面为index.html
	}
	}

	server{
	listen 83;    #监听83端口
	server_name localhost;    #域名为www.test.com
	location = / {    #精准匹配 / 接受/的请求,并重定向到 /data/nginx3
	root /data/nginx3;    #主目录为
	index index.html;    #默认页面为index.html
	}
	}

nginx -s reload    #重新加载

curl localhost:81    #此时返回默认界面
curl localhost:81/Nginx1    #此时因为是目录,我们需要在 /Nginx1/ 这样返回是正确的
curl localhost:82    #此时返回默认环境界面
curl localhost:82/nginx2    #此时输入一个路径就可以正常返回
curl localhost:83/    #此时返回欢迎界面,我们配置的精准匹配是不可能以 / 结尾的
相关推荐
搬码临时工20 分钟前
如何把本地服务器变成公网服务器?内网ip网址转换到外网连接访问
运维·服务器·网络·tcp/ip·智能路由器·远程工作·访问公司内网
vortex525 分钟前
探索 Shell:选择适合你的命令行利器 bash, zsh, fish, dash, sh...
linux·开发语言·bash·shell·dash
GalaxyPokemon1 小时前
LeetCode - 148. 排序链表
linux·算法·leetcode
Guheyunyi1 小时前
监测预警系统重塑隧道安全新范式
大数据·运维·人工智能·科技·安全
懒羊羊大王呀1 小时前
Ubuntu20.04中 Redis 的安装和配置
linux·redis
杰哥技术分享1 小时前
在 CentOS 上安装 Docker 和 Docker Compose 并配置使用国内镜像源
linux·docker·centos
知更鸟呆呆1 小时前
【Linux操作系统】基础开发工具(yum、vim、gcc/g++)
linux·运维·vim
xiangyong581 小时前
ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]
linux·ubuntu·gnu
Gold Steps.2 小时前
Docker容器部署elasticsearch8.*与Kibana8.*版本使用filebeat采集日志
运维·docker·云原生·es
m0_637146932 小时前
C语言基础面试问答
运维·服务器