Nginx虚拟主机

虚拟主机介绍:所谓的虚拟主机,在web服务器里就是一个独立的网络站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外服务供用户访问。

一、基于域名的虚拟主机

在这里插入图片描述
1、创建域名对应的站点目录文件

mkdir -p /data/xingyun/basic
mkdir -p /data/anttech/basic
chown -R nginx:nginx /data/xingyun/basic/
chown -R nginx:nginx /data/anttech/basic/

mkdir -p  /data/xingyun/log
mkdir -p  /data/anttech/log
chown -R nginx:nginx /data/xingyun/log/
chown -R nginx:nginx /data/anttech/log/

2、配置防火墙(先开启防火墙)

systemctl stop firewalld
sed -ri '/^SELINUX/c\SELINUX=disabled' /etc/selinux/config 
setenforce 0
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-servcie=https
firewall-cmd --reload
firewall-cmd --permanent --list-all

3、修改配置

cd /opt/data/nginx/conf/conf.d/

vi xingyun.conf

server {
    listen       192.168.65.153:80;
    server_name  www.xingyun.com;
    access_log /data/xingyun/log/access.log combined;
    location / {
        root   /data/xingyun/basic;
        index  index.html index.htm;
    }
}
vi anttech.conf 

server {
	listen		192.168.655.153:80;
	server_name	www.anttech.com;
	access_log	/data/anttech/log/access.log combined;
	location / {
		root /data/xingyun/basic;
		index index.html index.htm;
	}
}

4、域名解析
在本地hosts文件中添加域名信息
C:\Windows\System32\drivers\etc
在这里插入图片描述
5、结果测试
在/data/xingyun/basic 和/data/anttech/basic 目录下分别创建index.html 文件并添加内容

echo "this is a test from xingyun" >> /data/xingyun/basic/index.html

在这里插入图片描述

echo "this is a test from anttech" >> /data/anttech/basic/index.html

在这里插入图片描述

二、基于端口的虚拟主机

在这里插入图片描述

1、创建域名对应的站点目录文件

[root@localhost ~]# mkdir -p /data/xingyun1/basic
[root@localhost ~]# mkdir -p /data/xingyun2/basic
[root@localhost ~]# chown -R nginx:nginx /data/xingyun1/basic/
[root@localhost ~]# chown -R nginx:nginx /data/xingyun2/basic/
[root@localhost ~]# mkdir /data/xingyun1/log
[root@localhost ~]# mkdir /data/xingyun2/log
[root@localhost ~]# chown -R nginx:nginx /data/xingyun1/log/
[root@localhost ~]# chown -R nginx:nginx /data/xingyun2/log/


2、配置防火墙

[root@localhost ~]# systemctl start firewalld
You have new mail in /var/spool/mail/root
[root@localhost ~]#  firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --query-port=8080/tcp --permanent
yes
[root@localhost ~]# firewall-cmd --zone=public --add-port=6969/tcp --permanent
success
[root@localhost ~]# firewall-cmd --zone=public --query-port=6969/tcp --permanent
yes
[root@localhost ~]# firewall-cmd --reload
success

3、修改配置文件

cd /opt/data/nginx/conf/conf.d/
[root@localhost conf.d]# cat xingyun1.com
server {
        listen          192.168.65.153:8080;
        server_name     www.xingnyun.com;
        access_log      /data/xingyun1/log/aeecss.log combined;
        location / {
                root /data/xingyun1/basic;
                index index.html index.htm;
        }
}
[root@localhost conf.d]# cat xingyun2.conf
server {
        listen          192.168.65.153:6969;
        server_name     www.xingnyun.com;
        access_log      /data/xingyun2/log/access.log combined;
        location / {
                root /data/xingyun2/basic;
                index index.html index.htm;
        }
}

4、域名解析
在本地hosts文件中添加域名信息
C:\Windows\System32\drivers\etc

192.168.65.153 www.xingyun.com

5、结果测试

echo "This is a test from 8080..." >> /data/xingyun1/basic/index.html

在这里插入图片描述

echo "This is a test from 6969..." >> /data/xingyun2/basic/index.html

在这里插入图片描述

三、基于IP的虚拟主机

在这里插入图片描述
1、创建域名对应的站点目录文件

2、配置防火墙

3、在网卡上增加多个IP

4、修改配置

5、结果测试

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值