您的位置:首页 > 运维架构 > Nginx

配置Nginx虚拟主机

2019-01-19 14:56 1261 查看

    什么是虚拟主机?

    虚拟主机技术是指在一台物理主机服务器上划分出多个磁盘空间,每个磁盘空间都是一台虚拟主机,每台虚拟主机都可以对外提供Web服务,且互不干扰。用户能够利用虚拟主机把多个不同域名的网站部署在同一台服务器上,一台服务器上可以划分多台虚拟主机,同时每台虚拟主机上都可以部署并运行网站,而不必再为建立一个网站单独购买一台服务器,既解决了维护服务器技术的难题,同时又极大地节省了服务器硬件成本和相关的维护费用。

   基于端口号配置虚拟主机

    一个Nginx监听多个端口,根据不同的端口号,来区分不同的网站。

   在http块中添加serve块即可,配置如下:

    server {

        listen 8001;

        server_name 192.168.12.128;

        root  html/html8001;

        index index.html  index.htm;

      }

     server {

        listen 8002;

        server_name 192.68.12.128;

        root html/html8002;

        index index.html index.htm;

   }

......

测试效果如下:

基于IP配置Nginx虚拟主机:

1.设置IP别名

1).修改网络配置文件

[root@Nginx network-scripts]# cp  ifcfg-eth0 ifcfg-eth0:1

[root@Nginx network-scripts]# cp  ifcfg-eth0 ifcfg-eth0:2

[root@Nginx network-scripts]# vi ifcfg-eth0:1

DEVICE=eth0:1   

HWADDR=00:0C:29:13:04:9C

ONBOOT=yes

BOOTPROTO=static

TYPE=Ethernet

IPADDR=192.168.12.131

NETMASK=255.255.255.0

GATEWAY=192.168.12.2

#DNS=220.248.192.12

DNS=202.101.224.68

[root@Nginx network-scripts]# vi ifcfg-eth0:2

DEVICE=eth0:2

HWADDR=00:0C:29:13:04:9C

ONBOOT=yes

NM_CONTROLLED=yes

BOOTPROTO=static

TYPE=Ethernet

IPADDR=192.168.12.132

[root@Nginx network-scripts]# service network restart

正在关闭接口 eth0:                                        [确定]

关闭环回接口:                                             [确定]

弹出环回接口:                                             [确定]

弹出界面 eth0: Determining if ip address 192.168.12.128 is already in use for device eth0...

Determining if ip address 192.168.12.131 is already in use for device eth0...

Determining if ip address 192.168.12.132 is already in use for device eth0...

                                                           [确定]

弹出界面 eth0.back: RTNETLINK answers: File exists

                                                           [确定]

[root@Nginx network-scripts]# ifconfig  eth0:1 192.168.12.131 broadcast 192.168.12.2 netmask 255.255.255.0 up

[root@Nginx network-scripts]# route add -host 192.168.12.131 dev eth0:1 

[root@Nginx network-scripts]# ifconfig  eth0:2  192.168.12.132  broadcast 192.168.12.2 netmask 255.255.255.0 up  

[root@Nginx network-scripts]# route add -host 192.168.12.132 dev eth0:2

通过ifconfig和route配置的IP别名在系统网络服务重启后就会消失。

为了解决上述问题,可以将ifconfig和route命令添加到/etc/rc.local文件中,使系统开机时就会自动运行相关操作

[root@Nginx network-scripts]# vi /etc/rc.local        #在文件末尾添加下列几条命令

ifconfig  eth0:1 192.168.12.131 broadcast 192.168.12.2 netmask 255.255.255.0 up

route add -host 192.168.12.131 dev eth0:1 

ifconfig  eth0:2 192.168.12.132 broadcast 192.168.12.2 netmask 255.255.255.0 up

route add -host 192.168.12.132  dev eth0:2

2.配置基于IP的虚拟主机

[ro b68 ot@Nginx html]# mkdir 192.168.12.131  #在该目录下创建index.html测试文件

[root@Nginx html]# mkdir 192.168.12.132   #在该目录下创建index.html测试文件

修改nginx.conf配置文件,在http块中添加以下两个server配置,具体如下:

  server {

       listen 80;

       server_name 192.168.12.131;

       root    html/192.168.12.131;

      index  index.html  index.htm;

   }

  server {

       listen 80;

       server_name 192.168.12.132;

       root    html/192.168.12.132;

      index  index.html  index.htm;

   }

测试效果如下:






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Nginx Web;