栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Nginx虚拟主机

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Nginx虚拟主机

前言

利用虚拟主机,不用为每个要运行的网站提供一台单独的Nginx服务器或单独运行一组Nginx进程,虚拟主机提供了在一台服务器、同一组Nginx进程运行多个网站的功能。虚拟主机的基于三种类型

ip、域名、端口去进行流量分割来达成一台Nginx服务器满足多个网页的运行需求

一、基于域名的 Nginx 虚拟主机 1.为虚拟主机提供域名解析
echo "192.168.90.4 www.ghr.com www.accp.com" >> /etc/hosts
2.为虚拟主机准备网页文档
mkdir -p /var/www/html/ghr
mkdir -p /var/www/html/accp
echo "www.ghr.com" > /var/www/html/ghr/index.html
echo "www.accp.com" > /var/www/html/accp/index.html
3.修改Nginx的配置文件
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.ghr.com;					#设置域名www.xjj.com
		charset utf-8;
		access_log logs/www.ghr.access.log; 
		location / {
			root /var/www/html/ghr;					#设置www.xjj.com 的工作目录
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 80;
		server_name www.accp.com;					#设置域名www.accp.com
		charset utf-8;
		access_log logs/www.accp.access.log; 
		location / {
			root /var/www/html/accp;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}
4.重启服务,访问测试
systemctl restart nginx

浏览器访问:
http://www.ghr.com
http://www.accp.com
二、基于IP的虚拟主机
ifconfig ens33:0 192.168.90.100 netmask 255.255.255.0 

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
		listen 192.168.90.3:80;					#设置监听地址
		server_name www.accp.com;
		charset utf-8;
		access_log logs/www.accp.access.log; 
		location / {
			root /var/www/html/accp;
			index index.html index.php;
		}
}

server {
	listen 192.168.90.100:80;					#设置监听地址
	server_name www.accp100.com;
	charset utf-8;
	access_log logs/www.accp100.access.log; 
	location / {
		root /var/www/html/accp100;
		index index.html index.htm;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
		root html;
	}
}	
}
nginx -t
systemctl restart nginx.service
三、基于端口的虚拟主机
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
    server {
        listen 192.168.90.3:8080;
        server_name www.ghr.com;
        charset utf-8;
        access_log logs/www.ghr.access.log;
        location / {
            root /var/www/html/ghr;
            index index.html index.html;
        }
       
    }

    server {
        listen 192.168.90.3:80;
        server_name www.accp.com;
        charset utf-8;
        access_log logs/www.accp.access.log;
        location / {
            root /var/www/html/accp;
            index index.html index.php; 
        }
        error_page 500 502 503 504 /50x.html;
        location = 50x.html{
            root html;
        }
    }
}

nginx -t
systemctl restart nginx.service


总结
  • 虚拟主机:节省资源, 充分利用资源
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/321760.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号