Nginx的虚拟主机配置

时间:2021-08-01 14:48:17

虚拟主机技术能够让同一台服务器、同一组Nginx进程上运行多个网站,降低了资金和服务器资源的损耗。Nginx可以配置三种类型的虚拟主机,本文就是主要介绍这三种虚拟主机配置方式。

配置基于IP的虚拟主机

基于IP的虚拟主机就是直接把server_name设置为不同IP即可完成配置

http
{
server
{
listen 80;
server_name 192.168.6.53;
access_log logs/server1.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server1
}
}
server
{
listen 80;
server_name 192.168.6.54;
access_log logs/server2.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server2
}
}
server
{
listen 80;
server_name 192.168.6.55;
access_log logs/server3.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server3
}
}
}

基于域名的虚拟主机

基于域名的虚拟主机就是直接把server_name设置为不同域名即可完成配置

http
{
server
{
listen 80;
server_name a.domain.com;
access_log logs/a.domain.com.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server1
}
}
server
{
listen 80;
server_name b.domain.com;
access_log logs/b.domain.com.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server2
}
}
server
{
listen 80;
server_name www.domain domain.com *.domain.com;
access_log logs/server3.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server3
}
}
}

基于端口的虚拟主机

基于端口的虚拟主机就是直接把listen设置成不同的端口即可完成配置

http
{
server
{
listen 80;
server_name 192.168.6.53
access_log logs/server1.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server1
}
}
server
{
listen 8080;
server_name 192.168.6.53
access_log logs/server2.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server2
}
}
server
{
listen 10080;
server_name 192.168.6.53
access_log logs/server3.access.log commbined; location /
{
index index.html;
root/data1/wwwroot/server3
}
}
}