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/    #此时返回欢迎界面,我们配置的精准匹配是不可能以 / 结尾的
相关推荐
YC运维3 小时前
Dockerfile实战案例详解
运维·docker·容器
一个响当当的名号3 小时前
一些主要应用和NAT
运维·服务器·网络
@小博的博客3 小时前
【Linux探索学习】第二篇Linux的基本指令(2)——开启Linux学习第二篇
linux·运维·学习
openHiTLS密码开源社区5 小时前
【密码学实战】openHiTLS passwd命令行:专业密码哈希生成工具
linux·密码学·哈希算法·ldap·密码策略·随机盐值
WTCLLB5 小时前
netgear r6220 路由器,刷openwrt后,系统备份还原
linux·网络·智能路由器·openwrt
小灰灰的可爱无人可替代6 小时前
记录一次使用docker和docker-compose更新vue前端项目问题
nginx·docker·vue
迎風吹頭髮6 小时前
UNIX下C语言编程与实践38-UNIX 信号操作:signal 函数与信号捕获函数的编写
linux·c语言·unix
做运维的阿瑞7 小时前
Linux系统性能监控与故障定位实战:CPU/内存/I/O/网络
linux·运维·网络
驱动探索者7 小时前
车库到双子星:惠普的百年科技传奇
linux
wanhengidc7 小时前
云手机能够做些什么?
运维·服务器·人工智能·智能手机·云计算