码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx入门篇(三)之虚拟主机配置

时间:2018-07-17 18:13:23      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:agent   网站   body   events   host   测试   www.   process   nginx   

  • 一、虚拟主机概念

  所谓虚拟主机,在Web服务当中就是一个独立的网站站点,这个站点对应独立的域名(也有可能是IP或者端口),具有独立的程序和资源目录,可以独立地对外提供服务供用户访问。

  这个独立的站点在配置里是由一定格式的标签进行标记,和apache相对比,apache的虚拟主机的标签段通常是以<VirtualHost></VirtualHost>进行标注的,而Nginx则是以Server{}标签段来标示一个虚拟主机。一个Web服务中支持多个虚拟主机站点。

  • 二、虚拟主机类型

和apache一样,虚拟主机主要有3种:

(1)基于域名的虚拟主机

(2)基于端口的虚拟主机

(3)基于IP的虚拟主机

  • 三、三种虚拟主机类型配置演练

(1)基于域名域名的虚拟主机配置

(1)修改主配置文件nginx.conf,加载虚拟主机配置
[root@localhost conf]# grep -Ev "^$|#" nginx.conf user nginx; worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main $remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"; sendfile on; tcp_nopush on; keepalive_timeout 65; include /usr/local/nginx/conf/vhosts/*.conf;  #包含虚拟主机配置 }

(2)创建虚拟主机配置文件,并增加虚拟主机 [root@localhost conf]# mkdir vhosts && cd vhosts/ [root@localhost vhosts]# vim www.abc.org.conf server { listen 80; server_name www.abc.org; root /vhosts/html/www; index index.html index.htm index.php; } [root@localhost vhosts]# cp www.abc.org.conf bbs.abc.org.conf [root@localhost vhosts]# cp www.abc.org.conf blog.abc.org.conf [root@localhost vhosts]# vim bbs.abc.org.conf server { listen 80; server_name bbs.abc.org; root /vhosts/html/bbs; index index.html index.htm index.php; } [root@localhost vhosts]# vim blog.abc.org.conf server { listen 80; server_name blog.abc.org; root /vhosts/html/blog; index index.html index.htm index.php; }

(3)创建虚拟主机主页 [root@localhost vhosts]# mkdir /vhosts/html/{www,bbs,blog} [root@localhost vhosts]# echo "welcome to www.abc.org" >> /vhosts/html/www/index.html [root@localhost vhosts]# echo "welcome to bbs.abc.org" >> /vhosts/html/bbs/index.html [root@localhost vhosts]# echo "welcome to blog.abc.org" >> /vhosts/html/blog/index.html

(4)检查语法,重载nginx [root@localhost vhosts]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# /usr/local/nginx/sbin/nginx -s reload

windows下做hosts解析

192.168.56.11  www.abc.org bbs.abc.org blog.abc.org  分别访问

技术分享图片技术分享图片技术分享图片

(2)基于端口的虚拟主机配置

(1)修改bbs和blog站点监听端口
[root@localhost vhosts]# vim bbs.abc.org.conf listen
8081; [root@localhost vhosts]# vim blog.abc.org.conf listen 8082 [root@localhost vhosts]# export PATH=/usr/local/nginx/sbin/:$PATH

(2)检查语法,重载nginx [root@localhost vhosts]# nginx
-t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# nginx -s reload

(3)测试访问页面 [root@localhost
~]# curl www.abc.org welcome to www.abc.org [root@localhost ~]# curl bbs.abc.org:8081 welcome to bbs.abc.org [root@localhost ~]# curl blog.abc.org:8082 welcome to blog.abc.org

以上端口可以随意更改,但是不能和已有服务冲突,原则上应该是大于1024小于65535的任意端口

(3)基于IP的虚拟主机配置

(1)增加虚拟网卡eth0:0和eth0:1
[root@localhost ~]# ifconfig eth0:0 192.168.56.110/24 up [root@localhost ~]# ifconfig eth0:1 192.168.56.111/24 up [root@localhost ~]# ifconfig eth0:0 eth0:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.110 netmask 255.255.255.0 broadcast 192.168.56.255 ether 00:0c:29:ce:31:fd txqueuelen 1000 (Ethernet) [root@localhost ~]# ifconfig eth0:1 eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.111 netmask 255.255.255.0 broadcast 192.168.56.255 ether 00:0c:29:ce:31:fd txqueuelen 1000 (Ethernet)

(2)修改虚拟主机配置server_name为ip访问 [root@localhost vhosts]# vim bbs.abc.org.conf listen
8081; server_name 192.168.56.110; [root@localhost vhosts]# vim blog.abc.org.conf listen 8082; server_name 192.168.56.111;

(3)检测语法,重载nginx,测试访问 [root@localhost vhosts]# nginx
-t nginx: the configuration file /usr/local/nginx1.15.1/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx1.15.1/conf/nginx.conf test is successful [root@localhost vhosts]# nginx -s reload [root@localhost ~]# curl http://192.168.56.110:8081/ welcome to bbs.abc.org [root@localhost ~]# curl http://192.168.56.111:8082/ welcome to blog.abc.org

 

Nginx入门篇(三)之虚拟主机配置

标签:agent   网站   body   events   host   测试   www.   process   nginx   

原文地址:https://www.cnblogs.com/linuxk/p/9324393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!