Nginx:Nginx网站服务(二)

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

1.1 为虚拟主机提供域名解析

echo "192.168.119.10 www.kgc.com www.benet.com" >> /etc/hosts

1.2 为虚拟主机准备网页文档

mkdir -p /var/www/html/benet
mkdir -p /var/www/html/kgc
echo "<h1>www.kgc.com</h1>" > /var/www/html/kgc/index.html
echo "<h1>www.benet.com</h1>" > /var/www/html/benet/index.html

1.3 修改Nginx的配置文件

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

1.4.重启服务,访问测试

systemctl restart nginx

浏览器访问
http://www.kgc.com
http://www.benet.com

二、基于IP 的 Nginx 虚拟主机

ifconfig ens33:0 192.168.119.10 netmask 255.255.255.0 

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


systemctl restart nginx

浏览器访问
http://192.168.119.10
http://192.168.119.11

三、基于端口的 Nginx 虚拟主机

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


systemctl restart nginx

浏览器访问
http://192.168.119.10:8080
http://192.168.119.10:8888
  • Nginx 和 Apache 的差异
    1、轻量级,nginx比apache 占用更少的内存及资源;
    2、静态处理,Nginx 静态处理性能比 Apache 高 ;
    3、Nginx可以实现无缓存的反向代理加速,提高网站运行速度;
    4、Nginx的性能和可伸缩性不依赖于硬件,Apache依赖于硬件;
    5、Nginx支持热部署,启动速度迅速,可以在不间断服务的情况下,对软件版本或者配置进行升级;
    6、nginx是异步进程,多个连接可以对应一个进程 ;apache是同步多进程,一个连接对应一个进程;
    7、Nginx高度模块化,编写模块相对简单,且组件比Apache少;
    8、高并发下nginx 能保持低资源低消耗高性能;
    9、Nginx 配置简洁, Apache配置复杂。

猜你喜欢

转载自blog.csdn.net/2301_76875445/article/details/131032534