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/    #此时返回欢迎界面,我们配置的精准匹配是不可能以 / 结尾的
相关推荐
Sally璐璐2 小时前
LVS-DR模式高性能负载均衡实战
运维
路由侠内网穿透2 小时前
本地部署 SQLite 数据库管理工具 SQLite Browser ( Web ) 并实现外部访问
运维·服务器·开发语言·前端·数据库·sqlite
CodeHackerBhx3 小时前
Jenkins
java·运维·jenkins
小白不想白a3 小时前
【docker】namespace 命名空间
运维·docker·容器
小z博客3 小时前
使用Docker轻松部署Zurl搭建自己的短链接服务
运维·docker·容器
橘颂TA4 小时前
【Linux】特效爆满的Vim的配置方法 and make/Makefile原理
linux·运维·服务器·vim
AI大法师4 小时前
企业级Linux服务器安全:防火墙规则配置与Web/SSH服务优化指南
linux·服务器·安全
Fanmeang4 小时前
VGMP(VRRP Group Management Protocol)VRRP组管理协议
运维·网络·华为·防火墙·vrrp·双机热备·vgmp
花小璇学linux5 小时前
imx6ull-驱动开发篇10——pinctrl 子系统
linux·驱动开发·imx6ull·嵌入式软件
Hat_man_5 小时前
如何在虚拟机(Linux)安装Qt5.15.2
linux·运维·服务器