nginx介绍(三) - 虚拟主机
前言
前面提到过, 由nginx来分发请求到tomcat中, 那么怎么来区分这些tomcat呢?
我们一般访问网站的时候, 是不是可以使用 ip : port (127.0.0.1:8080)的方式来访问, 或者是 域名 : port (www.baidu.com:80), 只不过这里可以不写端口, 这是由于使用了默认的端口.
那么在nginx分发的时候, 是不是也可以通过 区分 域名 和 port 的方式来区分使用tomcat呢?
注: 同一个ip下面, 可以绑定多个域名, 但是一个域名, 只能有一个ip. 如果一个ip上面绑定了多个域名, 假如 127.0.0.1 绑定了 www.hao123.com 和 www.google.com, 那么在访问的时候, 给人的感觉, 是不是好像是我访问了不同的服务器, 并且, 他们都是使用默认80端口访问的.
一. 通过端口区分虚拟主机
1. 将nginx/html文件夹拷贝几份
[root@localhost nginx]# cp -r html html-8081 [root@localhost nginx]# cp -r html html-8082
接下来, 修改 html-8081, html-8082 下面的index.html文件
在欢迎的地方, 加上了各自的端口显示.
2. 修改配置文件
在server节点下面, 继续添加两个server节点, 主要修改其 listen , location.root 这两个地方
server {listen 8081;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main; location / {root html-8081;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html# error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }server {listen 8082;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main; location / {root html-8082;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html# error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }
3. 刷新配置
[root@localhost sbin]# ./nginx -s reload
4. 查看结果
同一个ip下, 通过不同端口, 确实访问到了不同的页面.
二. 通过域名来区分
1. 将html再复制几个
[root@localhost nginx]# cp -r html html-hao123 [root@localhost nginx]# cp -r html html-google
2. 为默认访问的index.html加一个小尾巴
3. 修改nginx配置文件
server { listen 80; server_name www.hao123.com;#charset koi8-r;#access_log logs/host.access.log main; location / { root html-hao123;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html# error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }server { listen 80; server_name www.google.com;#charset koi8-r;#access_log logs/host.access.log main; location / { root html-google;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html# error_page 500 502 503 504 /50x.html;location = /50x.html {root html;} }
4. 刷新配置
[root@localhost sbin]# ./nginx -s reload
5. 修改客户端的host文件, 将www.baidu.com , www.google.com 映射进去
6. 验证结果
乍一看, 我访问的是 hao123 和 google 啊, 怎么跑到我部署的nginx里面去了呢. 罒ω罒
转载于:https://www.cnblogs.com/elvinle/p/8268112.html
nginx介绍(三) - 虚拟主机相关推荐
- Nginx安装与虚拟主机配置shell脚本
今天继续给大家介绍Linux运维相关知识,本文主要内容是Nginx安装与虚拟主机配置shell脚本. 一.NGINX安装 在今天的脚本上,我们开始尝试使用函数和参数,以增强脚本的灵活性和实现脚本的模块 ...
- Nginx多站点虚拟主机实现单独启动停止php-fpm、单独控制权限设置
Nginx多站点虚拟主机实现单独启动停止php-fpm.单独控制权限设置 来源:osyunwei.com 作者:qihang01 发表于:2012-08-19 21:26 点击: 说明: 站点1:bb ...
- Nginx运行控制虚拟主机和访问控制
目录 一.Nginx服务概述 二.Nginx安装 三.Nginx的运行控制 四.配置文件nginx.conf 1.全局配置 2.I/O事件配置 3.HTTP配置 4.访问状态统计 五.Nginx访问控 ...
- Nginx优化之虚拟主机
文章目录 前言 一:Nginx服务基础 1.1:Nginx概述 1.2:Nginx编译安装(过程) 1.3:运行控制(实验过程) 1.4:配置文件nginx.conf 1.5:Nginx的访问状态统计 ...
- Nginx实现多虚拟主机配置
一.Nginx的应用概述 Nginx作为一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器.主要有以下3方面的应用: 1.http服务器 Nginx是一个http服 ...
- 简单介绍apache虚拟主机配置的三种方式
本文主要介绍了apache虚拟主机配置的三种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧 记事本打开httpd.conf文 ...
- 通过ngx-lua来统计Nginx上的虚拟主机性能数据
Web server调研分析 Filed under: Web Server - cmpan @ 2012-10-29 20:38:34 摘要 简单可依赖的架构首先需要有一个简单可依赖的前端WebSe ...
- 11-4 12 Nginx安装 默认虚拟主机 用户认证 域名重定向
2019独角兽企业重金招聘Python工程师标准>>> 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向 扩展 ngin ...
- Nginx——安装与虚拟主机配置(域名非80端口问题)
一.引言 因为甲方提出问题:一个服务器与IP,两个域名a.b分别对应两个要部署的网站,这个一般是一个域名a对应一个主机服务器ip的默认80端口,另外的网站只能用其他端口(如b:8080对应主机ip:8 ...
最新文章
- Delphi控件之---UpDown以及其与TEdit的配合使用(比如限制TEdit只能输入数字,还有Object Inspector之组件属性的介绍)...
- (转)细说Cookie
- employee.java,Java基础系列(六):对象与类(上)
- fastlane use_legacy_build_api true
- .NET下的验证码控件John.Controls.ValidateCode2V for .NET beta1
- 服务端增加WCF服务全局异常处理机制
- OPPO Reno 2正式发布:主打4800万变焦四摄 售价2999元
- Python 模块(module)
- Android Studio 下载 与 安装 详细步骤
- 大数据实效_新都区:运用“大数据”提升专项巡察实效
- CFA - Ethical Professional Standards 职业伦理道德
- 华三交换机配置DHCP中继
- 基于lingo的线性回归和非线性回归
- 第六周作业1——利用哈夫曼编码英文字母表
- getImageMatrix、getMatrix傻傻分不清
- 软磁材料的环境敏感性
- “寒江独钓”错误列表
- 嵌入式linux开发笔记——Ubuntu的使用
- JavaScript实现贷款利息计算器
- 问题解决:VScode在使用中文输入法时出现vim insert模式变成vim normal的奇怪现象( jj 映射成esc)