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/    #此时返回欢迎界面,我们配置的精准匹配是不可能以 / 结尾的
相关推荐
爱奥尼欧10 分钟前
【Linux 系统】基础IO——Linux中对文件的理解
linux·服务器·microsoft
超喜欢下雨天38 分钟前
服务器安装 ros2时遇到底层库依赖冲突的问题
linux·运维·服务器·ros2
搬码临时工44 分钟前
小企业如何搭建本地私有云服务器,并设置内部网络地址提供互联网访问
运维·服务器
tan77º1 小时前
【Linux网络编程】网络基础
linux·服务器·网络
18你磊哥2 小时前
Windows 本地安装部署 Apache Druid
运维·debian
笑衬人心。2 小时前
Ubuntu 22.04 + MySQL 8 无密码登录问题与 root 密码重置指南
linux·mysql·ubuntu
IT成长日记2 小时前
【Docker基础】Docker数据卷管理:docker volume inspect及其参数详解
运维·docker·容器·volume·inspect
ldj20202 小时前
Jenkins 构建过程常见错误
运维·jenkins
ladymorgana3 小时前
【Docker】如何设置 `wiredTigerCacheSizeGB` 和 `resources.limits.memory`
运维·docker·容器
chanalbert4 小时前
CentOS系统新手指导手册
linux·运维·centos