Nginx: Nginx website service (2)

1. Nginx virtual host based on domain name

1.1 Provide domain name resolution for virtual hosts

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

1.2 Prepare web documents for virtual hosting

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 Modify the configuration file of 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. Restart the service and access the test

systemctl restart nginx

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

2. IP-based Nginx virtual host

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

3. Port-based Nginx virtual host

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
  • Differences between Nginx and Apache :
    1. Lightweight, nginx occupies less memory and resources than Apache;
    2. Static processing, Nginx static processing performance is higher than Apache;
    3. Nginx can achieve reverse proxy acceleration without caching, improving Website running speed;
    4. The performance and scalability of Nginx do not depend on hardware, while Apache depends on hardware;
    5. Nginx supports hot deployment, fast startup speed, and can upgrade the software version or configuration without interrupting the service ;
    6. Nginx is an asynchronous process, and multiple connections can correspond to one process; Apache is a synchronous multi-process, and one connection corresponds to one process; 7. Nginx is highly
    modular, and writing modules is relatively simple, and has fewer components than Apache;
    8. High concurrency Downloading nginx can maintain low resource consumption and high performance;
    9. Nginx configuration is simple, while Apache configuration is complex.

Guess you like

Origin blog.csdn.net/2301_76875445/article/details/131032534