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 ;
}
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 ;
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 ;
[root@localhost vhosts]# vim blog.abc.org.conf
listen
[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:
welcome to bbs.abc.org
[root@localhost ~]# curl blog.abc.org:
welcome to blog.abc.org
以上端口可以随意更改,但是不能和已有服务冲突,原则上应该是大于1024小于65535的任意端口
(3)基于IP的虚拟主机配置
(1)增加虚拟网卡eth0:0和eth0:1
[root@localhost ~]# ifconfig eth0: 192.168.56.110/ up
[root@localhost ~]# ifconfig eth0: 192.168.56.111/ up
[root@localhost ~]# ifconfig eth0:
eth0:: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.56.110 netmask 255.255.255.0 broadcast 192.168.56.255
ether :0c::ce::fd txqueuelen (Ethernet) [root@localhost ~]# ifconfig eth0:
eth0:: flags=<UP,BROADCAST,RUNNING,MULTICAST> mtu
inet 192.168.56.111 netmask 255.255.255.0 broadcast 192.168.56.255
ether :0c::ce::fd txqueuelen (Ethernet) (2)修改虚拟主机配置server_name为ip访问
[root@localhost vhosts]# vim bbs.abc.org.conf
listen ;
server_name 192.168.56.110;
[root@localhost vhosts]# vim blog.abc.org.conf
listen ;
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入门篇(三)之虚拟主机配置的更多相关文章
- Nginx 反向代理 负载均衡 虚拟主机配置
Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...
- 【转】Nginx 反向代理 负载均衡 虚拟主机配置
原文:http://www.cnblogs.com/itdragon/p/8059000.html Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代 ...
- nginx 的三种虚拟主机配置方法
nginx三种虚拟主机配置的方法. 基于端口 在生产环境中一般使用端口或者域名. [root@web01 /etc/nginx/conf.d]# cat web01.conf server { lis ...
- 【nginx运维基础(2)】Nginx的配置文件说明及虚拟主机配置示例
配置文件说明 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型, ...
- Nginx 反向代理 负载均衡 虚拟主机
Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...
- Nginx的虚拟主机配置
虚拟主机技术能够让同一台服务器.同一组Nginx进程上运行多个网站,降低了资金和服务器资源的损耗.Nginx可以配置三种类型的虚拟主机,本文就是主要介绍这三种虚拟主机配置方式. 配置基于IP的虚拟主机 ...
- Ngnix 安装、信号量、虚拟主机配置
ngnix的安装很简单 1.先从ngnix官网下载ngnix压缩包 wget http://nginx.org/download/nginx-1.6.2.tar.gz 2.解压并进入其目录 tar - ...
- Nginx总结(三)基于端口的虚拟主机配置
前面讲了如何配置基于IP的虚拟主机,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天就 ...
- Nginx虚拟主机配置(三)
虚拟主机就是使用特殊的软硬件技术,把一台计算机主机分成多台“虚拟”的主机,每一台虚拟主机都具有独立的域名和IP地址(或共享的IP地址),具有完整的Internet服务器功能.在同一台硬件.同一个操作系 ...
随机推荐
- 四级菜单实现(Python)
menu_dict = { '山东' : { '青岛' : { '四方':{'兴隆路','平安路','杭州路'}, '黄岛':{}, '崂山':{} }, '济南' : { '历城':{}, '槐荫' ...
- 关于Mysql查询varchar类型错误问题
因为后台所有表ID都是按照雪花算法生成的18位数字,需要对接到Android,Ios和H5,此时H5会出现字符超长溢出,所以直接把ID改为varchar类型. 如我的一张表ID为varchar(18) ...
- C++内存总结——开坑,随时总结添加
C++内存区域分为: 程序代码区:存储程序代码的地方 栈区:编译器自动管理(分配释放)的内存区域,如函数参数,函数中的局部变量 堆区(又称动态存储区):由C语言中的函数malloc和free和C++ ...
- geth --rpcaddr
当想要实现从另一台电脑连接本电脑上开启的geth客户端时,需要将--rpcaddr设置为本电脑的ip地址,如下: geth --datadir data0 --networkid --port --r ...
- 使用Azcopy在Azure上进行HBase的冷热备份还原
场景 HBase表TaskLog中有20.55G数据(20553078551Byte),目前存放在热存储中,现在要移至冷热储,并进行还原. HBase目录:hbase/data/default 冷目录 ...
- centos 腾讯云 今天买了 18个月
1.安装开发编译工具包 yum groupinstall "Development Tools" 2.查看Linux 下用户 对系统资源的占用情况,root表示用户 top -u ...
- Gradle Goodness: Parse Files with SimpleTemplateEngine in Copy Task
With the copy task of Gradle we can copy files that are parsed by Groovy's SimpleTemplateEngine. Thi ...
- EF Core 2.0中如何手动映射数据库的视图为实体
由于Scaffold-DbContext指令目前还不支持自动映射数据库中的视图为实体,所以当我们想使用EF Core来读取数据库视图数据的时候,我们需要手动去做映射,本文介绍如何在EF Core中手动 ...
- 开源Webshell利用工具——Altman
开源Webshell利用工具--Altman keepwn @ 工具 2014-06-04 共 6114 人围观,发现 43 个不明物体收藏该文 Altman,the webshell tool,自己 ...
- Spring coud微服务框架具体实现关键说明
搭建一个微服务,考虑的问题涉及到运维,数据管理,性能,并发等方方面面.项目中使用Spring coud 搭建微服务,从技术选型,到技术实现都要全方面考虑服务化的问题.下面简单总结下搭建过程用的技术说明 ...