一、虚拟主机介绍javascript
现象css
一个web服务器软件默认状况下只能发布一个web,由于web分享出去须要三个条件IP、Port、Domainhtml
介绍html5
虚拟主机:就是把一台服务器划分红多个虚拟的服务器,每个虚拟主机均可以有独立的域名和独立的目录java
如何能发布多个web呢?node
二、基于IP虚拟主机python
ifconfig ens33:1 192.168.2.52/24 up
ifconfig ens33:1 down
同时发布两个网站:jquery
DocumentRoot: /usr/local/nginx/html/web2linux
server { listen 192.168.2.42:80; server_name localhost; charset utf-8; location / { root html/web01; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 192.168.2.52:80; server_name localhost; charset utf-8; location / { root html/web02; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
缺点:须要多个ip,若是是公网IP,每一个IP都须要付费android
三、基于端口的虚拟主机
server { listen 80; server_name localhost; charset utf-8; location / { root html/web01; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 81; server_name localhost; charset utf-8; location / { root html/web02; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
缺点:只须要一个IP,可是端口你是没法告诉公网用户,没法适用于公网客户,适合内部用户
四、基于域名的虚拟主机
修改:/etc/hosts/
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.2.42 www.abc.com 192.168.2.42 www.def.com
修改:/nginx.conf/
server { listen 80; server_name www.abc.com; charset utf-8; location / { root html/web01; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name www.def.com; charset utf-8; location / { root html/web02; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
测试:
[root@Web01 nginx]# elinks http://www.abc.com -dump i am web01 [root@Web01 nginx]# elinks http://www.def.com -dump i am web02