Nginx 服务--虚拟主机

文章目录

  • 一、基于域名的 Nginx 虚拟主机
    • 1.为虚拟主机提供域名解析
    • 2.为虚拟主机准备网页文档
    • 3.修改Nginx的配置文件
    • 4.重启服务,访问测试
  • 二、基于IP的虚拟主机
  • 三、基于端口的虚拟主机
  • 总结

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

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

echo "192.168.126.13 www.xjj.com www.accp.com" >> /etc/hosts

mark

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

mkdir -p /var/www/html/xjj
mkdir -p /var/www/html/accp
echo "

www.xjj.com

"
> /var/www/html/xjj/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.xjj.com;					#设置域名www.xjj.com
		charset utf-8;
		access_log logs/www.xjj.access.log; 
		location / {
     
			root /var/www/html/xjj;					#设置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;
		}
	}	
}

Nginx 服务--虚拟主机_第1张图片

4.重启服务,访问测试

systemctl restart nginx

浏览器访问:
http://www.xjj.com
http://www.accp.com

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ewlkj4T7-1633612858832)(C:\Users\姜姜爱吃香菜\AppData\Roaming\Typora\typora-user-images\image-20210930200704497.png)]

二、基于IP的虚拟主机

ifconfig ens33:0 192.168.126.31 netmask 255.255.255.0 

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

server {
     
	listen 192.168.126.18: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.htm;
	}
	error_page 500 502 503 504 /50x.html;
	location = 50x.html{
     
		root html;
	}
}	
}
nginx -t
systemctl restart nginx.service

Nginx 服务--虚拟主机_第2张图片

浏览器访问
http://192.168.126.14
http://192.168.126.18

Nginx 服务--虚拟主机_第3张图片

Nginx 服务--虚拟主机_第4张图片

三、基于端口的虚拟主机

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

    server {
     
        listen 192.168.126.14:8888;
        #设置监听8888端口
        server_name www.xcf.com;
        charset utf-8;
        access_log logs/www.xcf.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;
        }
    }
}

nginx -t
systemctl restart nginx.service


浏览器访问
http://192.168.126.14:8080
http://192.128.126.14:8888
systemctl restart nginx

Nginx 服务--虚拟主机_第5张图片

Nginx 服务--虚拟主机_第6张图片

Nginx 服务--虚拟主机_第7张图片

浏览器访问
http://192.168.126.14:8080
http://192.168.126.14:8888

Nginx 服务--虚拟主机_第8张图片

Nginx 服务--虚拟主机_第9张图片

总结

  • 虚拟主机:节省资源, 充分利用资源

你可能感兴趣的:(web服务器集群,nginx,运维,php)