【0】README: nginx 可以配置多种类型的虚拟主机: 

类型1) 基于ip的虚拟主机;

类型2)基于域名的虚拟主机;

类型3)基于端口的虚拟主机;

【1】配置基于 ip 的虚拟主机的准备工作

step1) 先用 ifconfig 查看该服务器的ip地址:   本服务器有一块物理网卡设备 eth0 和 本地回环设备lo, eth0 的ip地址为 192.168.186.100 ,本地回环lo的ip地址为 127.0.0.1 ;

eth0      Link encap:Ethernet  HWaddr 00:0C:29:71:D7:1A  inet addr:192.168.186.100  Bcast:192.168.186.255  Mask:255.255.255.0inet6 addr: fe80::20c:29ff:fe71:d71a/64 Scope:LinkUP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1RX packets:1349 errors:0 dropped:0 overruns:0 frame:0TX packets:1155 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:139966 (136.6 KiB)  TX bytes:145180 (141.7 KiB)Interrupt:19 Base address:0x2000 lo        Link encap:Local Loopback  inet addr:127.0.0.1  Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING  MTU:16436  Metric:1RX packets:4 errors:0 dropped:0 overruns:0 frame:0TX packets:4 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b) 

step2)  在 eth0 网卡设备上 添加两个ip别名 192.168.186.101 和 192.168.186.102,可以通过以下的 ifconfig 和 route命令来进行:

[root@localhost nginx]# ifconfig eth0:1 192.168.186.101 broadcast 192.168.186.255 netmask 255.255.255.0 up
[root@localhost nginx]# route add -host 192.168.186.101 dev eth0:1
[root@localhost nginx]#
[root@localhost nginx]# ifconfig eth0:2 192.168.186.102 broadcast 192.168.186.255 netmask 255.255.255.0 up
[root@localhost nginx]# route add -host 192.168.186.102 dev eth0:2

step3)再执行 ifconfig命令,就可以看到 eth0 网卡设备上绑定两个 ip 别名,如下所示:

[pacoson@localhost ~]$ ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:71:D7:1A  inet addr:192.168.186.100  Bcast:192.168.186.255  Mask:255.255.255.0inet6 addr: fe80::20c:29ff:fe71:d71a/64 Scope:LinkUP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1RX packets:1349 errors:0 dropped:0 overruns:0 frame:0TX packets:1155 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:139966 (136.6 KiB)  TX bytes:145180 (141.7 KiB)Interrupt:19 Base address:0x2000 eth0:1    Link encap:Ethernet  HWaddr 00:0C:29:71:D7:1A  inet addr:192.168.186.101  Bcast:192.168.186.255  Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1Interrupt:19 Base address:0x2000 eth0:2    Link encap:Ethernet  HWaddr 00:0C:29:71:D7:1A  inet addr:192.168.186.102  Bcast:192.168.186.255  Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1Interrupt:19 Base address:0x2000 lo        Link encap:Local Loopback  inet addr:127.0.0.1  Mask:255.0.0.0inet6 addr: ::1/128 Scope:HostUP LOOPBACK RUNNING  MTU:16436  Metric:1RX packets:4 errors:0 dropped:0 overruns:0 frame:0TX packets:4 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:0 RX bytes:240 (240.0 b)  TX bytes:240 (240.0 b)

step4)ping 通, 证明配置无误

[pacoson@localhost ~]$ ping 192.168.186.101
PING 192.168.186.101 (192.168.186.101) 56(84) bytes of data.
64 bytes from 192.168.186.101: icmp_seq=1 ttl=64 time=4.35 ms
64 bytes from 192.168.186.101: icmp_seq=2 ttl=64 time=0.149 ms
64 bytes from 192.168.186.101: icmp_seq=3 ttl=64 time=0.073 ms
64 bytes from 192.168.186.101: icmp_seq=4 ttl=64 time=0.065 ms
64 bytes from 192.168.186.101: icmp_seq=5 ttl=64 time=0.063 ms
64 bytes from 192.168.186.101: icmp_seq=6 ttl=64 time=0.066 ms
64 bytes from 192.168.186.101: icmp_seq=7 ttl=64 time=0.070 ms
64 bytes from 192.168.186.101: icmp_seq=8 ttl=64 time=0.069 ms
^C
--- 192.168.186.101 ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7915ms
rtt min/avg/max/mdev = 0.063/0.613/4.352/1.413 ms
[pacoson@localhost ~]$
[pacoson@localhost ~]$
[pacoson@localhost ~]$ ping 192.168.186.102
PING 192.168.186.102 (192.168.186.102) 56(84) bytes of data.
64 bytes from 192.168.186.102: icmp_seq=1 ttl=64 time=9.36 ms
64 bytes from 192.168.186.102: icmp_seq=2 ttl=64 time=0.212 ms
64 bytes from 192.168.186.102: icmp_seq=3 ttl=64 time=0.065 ms
64 bytes from 192.168.186.102: icmp_seq=4 ttl=64 time=0.063 ms
64 bytes from 192.168.186.102: icmp_seq=5 ttl=64 time=0.064 ms
64 bytes from 192.168.186.102: icmp_seq=6 ttl=64 time=0.063 ms
64 bytes from 192.168.186.102: icmp_seq=7 ttl=64 time=0.063 ms
64 bytes from 192.168.186.102: icmp_seq=8 ttl=64 time=0.077 ms
64 bytes from 192.168.186.102: icmp_seq=9 ttl=64 time=0.062 ms
^C
--- 192.168.186.102 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8993ms
rtt min/avg/max/mdev = 0.062/1.115/9.366/2.917 ms 

【注意】ifconfig 和 route 配置的ip别名在服务器重启后会消失;可以将 ifconfig 和 route 命令添加到 /etc/rc.local 文件中, 让系统开机时自动运行, 以下是相关命令:

vim /etc/rc.local

在末尾增加以下内容,

ifconfig eth0:1 192.168.186.101 broadcast 192.168.186.255 netmask 255.255.255.0 up
route add -host 192.168.186.101 dev eth0:1
ifconfig eth0:2 192.168.186.102 broadcast 192.168.186.255 netmask 255.255.255.0 up
route add -host 192.168.186.102 dev eth0:2

【2】配置基于ip的虚拟主机

1)配置文件 nginx.conf 如下:

# this config is from 3-6
worker_processes 3;
events {worker_connections 1024;
}
http
{# first virtual host 第一个虚拟主机server{# ip and port monitored 监听的ip和端口listen          192.168.186.100:80;# host name 主机名称server_name    192.168.186.100;# the dir access_log saved 访问日志文件存放路径access_log      logs/server1.access.log combined;location /{# default index file, priority reduction from left to right# 默认首页文件,顺序从左到右,index index.html index.htm;# the dir html file saved, html网页文件存放目录root  /data0/htdocs/server1;}}# second virtual hostserver{listen          192.168.186.101:80;server_name    192.168.186.101;access_log      logs/server2.access.log combined;location /{index index.html index.htm;root  /data0/htdocs/server2;}}# third virtual hostserver {listen          192.168.186.102:80;server_name    192.168.186.102;access_log      logs/server3.access.log combined;location /{index index.html index.htm;root  /data0/htdocs/server3.com;}}
}

【3】配置基于域名的虚拟主机
1)nginx配置文件如下:

http
{# 第一个虚拟主机server{# 监听的端口listen     80;# 主机名称server_name    aaa.domain.com;# 访问日志文件存放路径access_log      logs/aaa.domain.com.access.log combined;location /{# 默认首页文件,顺序从左到右,如果找不到index.html文件,则查找index.htm文件作为首页文件index index.html index.htm;# HTML网页文件存放的目录root  /data0/htdocs/aaa.domain.com;}}# 第二个虚拟主机server{# 监听的IP和端口listen     80;# 主机名称server_name    bbb.otherdomain.com;# 访问日志文件存放路径access_log      logs/bbb.otherdomain.com.access.log combined;location /{# 默认首页文件,顺序从左到右,如果找不到index.html文件,则查找index.htm文件作为首页文件index index.html index.htm;# HTML网页文件存放的目录root  /data0/htdocs/bbb.otherdomain.com;}}# 第三个虚拟主机server{# 监听的IP和端口listen     80;# 主机名称server_name    www.domain.com domain.com *.domain.com;# 访问日志文件存放路径access_log      logs/bbb.domain.com.access.log combined;location /{# 默认首页文件,顺序从左到右,如果找不到index.html文件,则查找index.htm文件作为首页文件index index.html index.htm;# HTML网页文件存放的目录root  /data0/htdocs/domain.com;}}
}

nginx-配置基于ip或域名的虚拟主机相关推荐

  1. nginx配置基于端口、域名的虚拟主机

    基于域名 首先,进入主机电脑的C:\Windows\System32\drivers\etc目录 找到hosts文件,以管理员身份打开 添加172.16.11.82 www.yuancao.com 这 ...

  2. linux apache ip配置,linux apache虚拟主机配置(基于ip,端口,域名)

    配置环境: linux版本:Centos6.4 httpd版本: [[email protected] init.d]# pwd /etc/init.d [[email protected] init ...

  3. 1、基于ip地址的不同虚拟主机

    自定义配置文件 [root@www~]# cd /etc/httpd/conf.d/         [root@www yum.repos.d]# ll         [root@www yum. ...

  4. Nginx配置基于ip的虚拟主机

    我是在centos7虚拟机上进行实验的 该实验基于添加好ip的基础上,如何给网卡添加ip请查阅我的博客 先来看一下我的ip [root@localhost nginx]# ifconfig ens33 ...

  5. Nginx配置基于IP的访问控制

    基于ngx_http_access_module模块,默认可使用 语法: deny ip 禁止ip访问 allow ip 允许访问 写在nginx.conf的server段即可 示例: allow 1 ...

  6. linux基于域名的虚拟主机,Nginx虚拟主机应用——基于域名、IP、端口的虚拟主机...

    Nginx支持的虚拟主机有三种 ●基于域名的虚拟主机 ●基于IP的虚拟主机 ●基于端口的虚拟主机 每一种虚拟主机均可通过"server{}" 配置段实现各自的功能 基于域名的虚拟主 ...

  7. 十八.搭建Nginx服务器、配置网页认证、基于域名的虚拟主机、ssl虚拟主机

    配置要求: client:192.168.4.10 proxy:192.168.4.5(eth0) 192.168.2.5(eth1) web1:192.168.2.100 web2:192.168. ...

  8. 基于域名的虚拟主机配置

    更多详细文档:http://download.csdn.net/download/zml_2015/8843061 基于域名的虚拟主机的配置只需服务器有一个IP地址即可,所有的虚拟主机共享同一个IP, ...

  9. Apache配置虚拟主机----基于域名的虚拟主机技术

    Apache配置虚拟主机(基于域名的虚拟主机技术) 1.关闭防火墙 systemctl disable firewalld selinux 0 2.修改一下主配置文件 vi /usr/local/ap ...

最新文章

  1. linux ftp 团队认证,linux下ftp和ftps以及ftp基于mysql虚拟用户认证服务器的搭建
  2. 5个很常用的CSS3网页小实例
  3. Hadoop中shuffle阶段流程分析
  4. PyQt5基础——3
  5. js解析二维码_最新最全阿里巴巴,今日头条,腾讯Flutter面试真题全解析(狂虐不止)...
  6. java基础57 css样式、选择器和css定位(网页知识)
  7. ASP.NET MVC CheckBoxFor为什么会生成hidden input控件
  8. Spring Data对Cassandra 3的支持
  9. 适用于Windows的Xcode:在PC上安装Xcode的5种方法
  10. 声音场景分类问题探讨
  11. 软件工程复习笔记 类图
  12. 百度漂亮的金融营销仗
  13. 【FCPX插件】56种标题文本输入逐字出现字幕打字机动画效果 Typing Titles,支持M1芯片!
  14. Freemarker模板---引擎学习
  15. 股票自选股基本函数大全-7
  16. 关于很狗的军训qwq
  17. PN结正向压降温度特性的研究
  18. ORACLE 取唯一数据
  19. 那些被疯狂追求的女孩,后来怎么样了?
  20. 互联网+创新创业大赛

热门文章

  1. E - Flow Gym - 102471E
  2. 牛客题霸 [删除链表的倒数第n个节点] C++题解/答案
  3. 「ROI 2017 Day 2」反物质(单调队列优化dp)
  4. CF1039D-You Are Given a Tree【根号分治,贪心】
  5. P5325-[模板]Min_25筛
  6. ssl提高组周三备考赛【2018.10.17】
  7. 【LCT】魔法森林(P2387)
  8. 数学推导题,NTT,快速数论变换,Wannafly-乒乓球
  9. 在MySQL的InnoDB存储引擎中count(*)函数的优化
  10. java.nio.ByteBuffer用法小结