Nginx网络服务——虚拟主机设置

相比较Apache的虚拟主机设置,Nginx的设置是十分简便的只需要修改主配置中的相关配置就能实现虚拟主机的效果

一、基于域名的 Nginx 虚拟主机

1. 为虚拟主机提供域名解析

echo "192.168.190.40 www.gundam.com www.noelle.com" >> /etc/hosts

2.为虚拟主机准备网页文档

mkdir -p /var/www/html/ztm
mkdir -p /var/www/html/hss
echo "<h1>www.gundam.com</h1>" > /var/www/html/gundam/index.html
echo "<h1>www.noelle.com</h1>" > /var/www/html/noelle/index.html

 3.修改Nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 80;
		server_name www.gundam.com;					#设置域名www.gundam.com
		charset utf-8;
		access_log logs/www.gundam.access.log; 		#设置日志名
		location / {
			root /var/www/html/gundam;					#设置www.gundam.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.noelle.com;					#设置域名www.noelle.com
		charset utf-8;
		access_log logs/www.noelle.access.log; 
		location / {
			root /var/www/html/noelle;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

 4.重启服务,浏览器访问测试

 

二、基于IP 的 Nginx 虚拟主机

1.设置虚拟主机IP

ifconfig ens33:0 192.168.190.42 netmask 255.255.255.0

 2.修改主配置文件

vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.190.40:80;					#设置监听地址192.168.190.40
		server_name www.gundam.com;
		charset utf-8;
		access_log logs/www.gundam.access.log; 
		location / {
			root /var/www/html/gundam;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 192.168.190.42:80;					#设置监听地址192.168.190.42
		server_name www.noelle.com;
		charset utf-8;
		access_log logs/www.noelle.access.log; 
		location / {
			root /var/www/html/noelle;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

 3.重启服务,浏览器访问测试

重启服务
systemctl restart nginx

浏览器访问
http://192.168.190.40
http://192.168.190.42

 三、基于端口的Nginx虚拟机

1.修改主配置文件


vim /usr/local/nginx/conf/nginx.conf
......
http {
......
	server {
		listen 192.168.190.40:8080;					#设置监听 8080 端口
		server_name www.ztm.com;
		charset utf-8;
		access_log logs/www.gundam.access.log; 
		location / {
			root /var/www/html/gundam;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}
	
	server {
		listen 192.168.190.40:8888;					#设置监听 8888 端口
		server_name www.noelle.com;
		charset utf-8;
		access_log logs/www.noelle.access.log; 
		location / {
			root /var/www/html/noelle;
			index index.html index.php;
		}
		error_page 500 502 503 504 /50x.html;
		location = 50x.html{
			root html;
		}
	}	
}

 2.重启服务,浏览器访问测试

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值