0
点赞
收藏
分享

微信扫一扫

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

年迈的代码机器 2023-06-06 阅读 81

一、基于域名的 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 条评论