Nginx 虚拟主机配置的三种方式(一)(基于IP)

Nginx配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。

1、基于IP的虚拟主机配置

如果同一台服务器有多个IP,可以使用基于IP的虚机主机配置,将不同的服务绑定在不同的IP上。

1.1 假设服务器有个IP地址为192.168.2.150,首先使用ifconfig在同一个网络接口上绑定其他3个IP。
[root@localhost ~]# ifconfig ens33:1 192.168.2.151/24 up
[root@localhost ~]# ifconfig ens33:2 192.168.2.152/24 up
[root@localhost ~]# ifconfig ens33:3 192.168.2.153/24 up
[root@localhost ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.106  netmask 255.255.255.0  broadcast 192.168.2.255
        inet6 fe80::2a8d:be6:a4a8:ea0  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)
        RX packets 1220  bytes 87955 (85.8 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 206  bytes 23755 (23.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.151  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)

ens33:2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.152  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)

ens33:3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.153  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 72  bytes 6252 (6.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 72  bytes 6252 (6.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
1.2 3个IP对应的域名如下,配置主机的host文件便于测试
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.2.151 www.test151.com
192.168.2.152 www.test152.com
192.168.2.153 www.test153.com

可以模拟实现DNS轮询的情况。

附:设置完hosts文件后一定要记得执行以下命令使其生效

1、windows下cmd进入命令行

C:\Users\1234>ipconfig /flushdns

Windows IP 配置

已成功刷新 DNS 解析缓存。
1.3 建立虚拟主机存放网页的根目录,并创建首页文件index.html
[root@localhost /]# mkdir -p /data/www
[root@localhost /]# cd /data/www
[root@localhost www]# mkdir 151
[root@localhost www]# mkdir 152
[root@localhost www]# mkdir 153
[root@localhost www]# echo "192.168.2.151" > 151/index.html
[root@localhost www]# echo "192.168.2.152" > 152/index.html
[root@localhost www]# echo "192.168.2.153" > 153/index.html
[root@localhost www]# ls
151  152  153
1.4 修改nginx.conf,将虚拟主机配置文件包含进主文件
[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@localhost conf]# vim nginx.conf

在nginx.conf文件末尾加入以下配置

# 在http段中找到以下内容并删除每行前面的“#”
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

# 配置文件结尾的最后一个“}”之前加入以下语句,如下所示
include vhost/*.conf;
}
1.5 编辑每个IP的配置文件(每个虚拟主机的配置文件)
[root@localhost conf]# mkdir -p vhost
[root@localhost conf]# cd vhost/
[root@localhost vhost]# cat www.test151.conf
    server {
        listen       192.168.2.151:80;
        # 配置成实际的域名,每个虚拟主机的配置文件域名都相同
        #server_name  www.test.com;

        access_log   /data/logs/www.test151.com.log main;
        error_log    /data/logs/www.test151.com.error.log;

        location / {
            root   /data/www/151;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# cat www.test152.conf
    server {
        listen       192.168.2.152:80;
        # 配置成实际的域名,每个虚拟主机的配置文件域名都相同
        #server_name  www.test.com;

        access_log   /data/logs/www.test152.com.log main;
        error_log    /data/logs/www.test152.com.error.log;

        location / {
            root   /data/www/152;
            index  index.html index.htm;
        }
    }

[root@localhost vhost]# cat www.test153.conf
    server {
        listen       192.168.2.153:80;
        # 配置成实际的域名,每个虚拟主机的配置文件域名都相同
        #server_name  www.test.com;

        access_log   /data/logs/www.test153.com.log main;
        error_log    /data/logs/www.test153.com.error.log;

        location / {
            root   /data/www/153;
            index  index.html index.htm;
        }
    }
1.6 创建日志文件,否则无法启动nginx
[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.test151.com.log
[root@localhost /]# touch /data/logs/www.test151.com.error.log
[root@localhost /]# touch /data/logs/www.test152.com.log
[root@localhost /]# touch /data/logs/www.test152.com.error.log
[root@localhost /]# touch /data/logs/www.test153.com.log
[root@localhost /]# touch /data/logs/www.test153.com.error.log
[root@localhost /]# ls /data/logs/
www.test151.com.error.log  www.test152.com.error.log  www.test153.com.error.log
www.test151.com.log        www.test152.com.log        www.test153.com.log
1.7 先测试配置文件然后再启动nginx
[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 启动nginx
[root@localhost sbin]# ./nginx
1.8 测试文件
[root@localhost sbin]# curl www.test151.com
192.168.2.151
[root@localhost sbin]# curl www.test152.com
192.168.2.152
[root@localhost sbin]# curl www.test153.com
192.168.2.153

附:配置过程中出现的问题

1、测试配置文件时出现的问题
[root@localhost sbin]# ./nginx -t
nginx: [emerg] unexpected "}" in /usr/local/nginx/conf/nginx.conf:122
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

决解方法:下面语句忘记加分号

include vhost/*.conf;
2、使用 curl www.test*.com 测试时总是访问到相同的结果

解决方法:不要将IP地址写在 server_name 后面, server_name 后面只能添加域名。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值