nginx配置主机的三种方式

Nginx配置虚拟主机的三种方式

方式一、基于主机多IP方式 10.0.0.7 172.16.1.7
方式二、基于端口的配置方式 80 81 82 83
方式三、基于名称方式(多域名方式) test1 test2 test3(推荐使用)

  • 方式一:基于主机多IP方式
[root@web01 ]cd /etc/nginx/conf.d
[root@web01 conf.d]# cat ip_eth0.conf 
server {
	listen 10.0.0.7:80;
	location / {
		root /ip1;
		index index.html;
	}
}
server {
	listen 172.16.1.7:80;
	location / {
		root /ip2;
		index index.html;
	}
}
[root@web01 conf.d]# mkdir /ip1 /ip2
[root@web01 conf.d]# echo "10...." > /ip1/index.html
[root@web01 conf.d]# echo "172...." > /ip2/index.html
[root@web01 conf.d]# systemctl restart nginx


测试访问
[root@web01 ~]# curl http://10.0.0.7
10.... 
[root@web01 ~]# curl http://172.16.1.7
172....
  • 方式二: 基于端口的配置方式
公司内部有多套系统,希望部署在一台服务器上, 而内网又没有域名.
	所以,我们可以通过相同IP,不同的端口,访问不同的网站页面.
[root@web01 conf.d]# cat port.conf 
server {
	listen 81;

	location / {
		root /81;
		index index.html;
	}
}

server {
	listen 82;

	location / {
		root /82;
		index index.html;
	}
}

server {
	listen 83;

	location / {
		root /83;
		index index.html;
	}
}
[root@web01 conf.d]# mkdir /81 /82 /83
[root@web01 conf.d]# echo "81" > /81/index.html
[root@web01 conf.d]# echo "82" > /82/index.html
[root@web01 conf.d]# echo "83" > /83/index.html

案例:
三个网站运行在同一台服务器,只需要通过不同的域名来实现访问:
		game
		wzq
		tk
posted @   老王教你学Linux  阅读(213)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示