nginx虚拟主机的介绍及配置
虚拟主机的概念
所谓的虚拟主机,在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或者端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问
虚拟主机的类型
常见的虚拟主机类型有如下几种
基于域名的虚拟主机
所谓基于域名的虚拟主机,意思就是通过不同的域名来区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站都使用的基于域名的虚拟主机
基于端口的虚拟主机
同理,所谓基于端口的虚拟主机,意思就是通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站。
基于IP的虚拟主机
同理,所谓基于IP的虚拟主机,意思就是通过不同的IP区分不同的虚拟主机,此类虚拟主机企业应用非常少见
配置基于域名的nginx.conf内容
修改nginx.conf里面的内容
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
创建域名对应的站点目录及文件
[root@web01 conf]# mkdir ../html/www -p [root@web01 conf]# echo "http://www.etiantian.org" >../html/www/index.html [root@web01 conf]# cat ../html/www/index.html http://www.etiantian.org
上述命令的作用是创建了一个html/www站点目录,对应于虚拟主机配置文件里root根目录的html/www设置(root html/www).然后生成一个默认的首页文件index.html,内容是http://www.etiantian.org
检查语法并重新加载Nginx [root@web01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful [root@web01 conf]# ../sbin/nginx -s reload 查看一下是否开启成功 [root@web01 conf]# ps -ef |grep nginx root 7254 1 0 16:02 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx www 7255 7254 0 16:02 ? 00:00:00 nginx: worker process root 34266 7262 0 17:12 pts/1 00:00:00 grep --color=auto nginx 添加hosts解析关系 [root@web01 conf]# echo '10.0.0.8 www.etiantian.org' >>/etc/hosts [root@web01 conf]# tail -1 /etc/hosts 10.0.0.8 www.etiantian.org [root@web01 conf]# curl www.etiantian.org http://www.etiantian.org
针对windows客户端浏览器访问,如果域名没做DNS解析,可编辑电脑上的hosts文件,添加记录解析
windows客户端hosts文件的通用路径C:\Windows\System32\drivers\etc\hosts

到此我们的基于域名的配置方法已经成功
配置多个基于域名的虚拟主机
增加新域名对应的配置
下面标记颜色的是改过的
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 80; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
创建新虚拟主机站点对应的目录和文件
[root@web01 conf]# mkdir ../html/bbs ../html/blog -p [root@web01 conf]# echo 'http://bbs.etiantian.org' >../html/bbs/index.html [root@web01 conf]# echo 'http://blog.etiantian.org' >../html/blog/index.html 检查配置的结果 [root@web01 conf]# cat ../html/bbs/index.html http://bbs.etiantian.org [root@web01 conf]# cat ../html/blog/index.html http://blog.etiantian.org
检查语法,重新加载nginx配置
[root@web01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful [root@web01 conf]# ../sbin/nginx -s reload
在客户端测试
在linux客户端和windows客户端,添加hosts解析关系
[root@web01 conf]# tail -1 /etc/hosts
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org
windows浏览器测试结果

基于端口的虚拟主机配置实战
修改虚拟主机配置
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 81; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 82; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
检查语法重新加载配置生效
[root@web01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful [root@web01 conf]# ../sbin/nginx -s reload
测试不同的端口的访问结果

基于IP的虚拟主机配置实战
修改配置文件 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 172.16.1.41:80; server_name www.etiantian.org; location / { root html/www; index index.html index.htm; } } server { listen 172.16.1.31:81; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } } server { listen 172.16.1.51:82; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } } }
检查语法
[root@web01 conf]# ../sbin/nginx -t nginx: the configuration file /application/nginx-1.10.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.10.2/conf/nginx.conf test is successful
然后进行测试即可,这里就不一一演示了

浙公网安备 33010602011771号