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/    #此时返回欢迎界面,我们配置的精准匹配是不可能以 / 结尾的
相关推荐
笑稀了的野生俊35 分钟前
ImportError: /lib/x86_64-linux-gnu/libc.so.6: version GLIBC_2.32‘ not found
linux·人工智能·ubuntu·大模型·glibc·flash-attn
步、步、为营37 分钟前
.net服务器Kestrel配置Nginx作为反向代理
服务器·nginx·.net
千帐灯无此声1 小时前
Linux 测开:日志分析 + 定位 Bug
linux·c语言·c++·bug
誰能久伴不乏1 小时前
深入了解 Vim 编辑器:从入门到精通
linux·编辑器·vim
ghie90901 小时前
在Centos系统上如何有效删除文件和目录的指令汇总
linux·运维·centos
Linux-palpitate1 小时前
Keepalived+LVS实现LNMP网站的高可用部署
linux·运维·服务器·mysql·lvs
潇-xiao2 小时前
进程状态 + 进程优先级切换调度-进程概念(5)
linux·笔记
CZZDg2 小时前
LVS负载均衡-DR模式配置
运维·负载均衡·lvs
FileLink跨网文件交换2 小时前
跨网文件交换?内外网文件交换十大方法构建安全合规的数据传输通道
运维·服务器·网络
江公望3 小时前
Linux kernel regcache_cache_only()函数详解
linux