Nginx上配置虚拟机

基于IP地址的虚拟主机配置,在一台主机上有多个IP的情况。
不同的IP对应不同的网站内容,客户端可以通过IP访问不同的网页内容。
为服务器添加多个IP地址:
ifconfig eth0:1 192.168.2.5 netmask 255.255.255.0 up
ifconfig eth0:2 192.168.2.6 netmask 255.255.255.0 up

在/etc/nginx/conf.d目录下新建一个iphost.conf,内容如下
server
{
    listen       192.168.2.5:80;
    server_name 192.168.2.5;
    access_log  /var/log/nginx/iphost1.access.log  main;
    error_log  /var/log/nginx/iphost1.error.log warn;
    location / {
            root   /var/www/nginx/site1;
            index  index.html index.htm;
    }
}

server
{
    listen       192.168.2.6:80;
    server_name 192.168.2.6;
    access_log  /var/log/nginx/iphost2.access.log  main;
    error_log  /var/log/nginx/iphost2.error.log warn;
    location / {
            root   /var/www/nginx/site2;
            index  index.html index.htm;
    }
}
在/etc/nginx/conf.d目录下新建一个vhost.conf,内容如下
基于主机头的虚拟主机配置,Web服务器上只有一个IP,
服务器根据客户端请求的host判断请求的是哪一个web站点。
server
{
    listen       80;
    server_name tec.test.com;
    access_log  /var/log/nginx/iphost1.access.log  main;
    error_log  /var/log/nginx/iphost1.error.log warn;
    location / {
            root   /var/www/nginx/site1;
            index  index.html index.htm;
    }
}

server
{
    listen       80;
    server_name mkt.test.com;
    access_log  /var/log/nginx/iphost2.access.log  main;
    error_log  /var/log/nginx/iphost2.error.log warn;
    location / {
            root   /var/www/nginx/site2;
            index  index.html index.htm;
    }
}
如果没有配置DNS,可以修改客服端host文件来测试。
通过tec.test.com访问的则为//var/www/nginx/site1的内容。
通过mkt.test.com访问的则为/var/www/nginx/site2的内容。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值