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 ;
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 ;
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里面去了呢. 罒ω罒
nginx介绍(三) - 虚拟主机的更多相关文章
- 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置
第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本 uwsgi- ...
- Nginx(二):虚拟主机配置
什么是虚拟主机? 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功 ...
- Nginx多站点虚拟主机实现单独启动停止php-fpm、单独控制权限设置
Nginx多站点虚拟主机实现单独启动停止php-fpm.单独控制权限设置 来源:osyunwei.com 作者:qihang01 发表于:2012-08-19 21:26 点击: 说明: 站点1:bb ...
- linux上nginx上配置虚拟主机的相关配置
1.配置主配置: nginx/conf/nginx.conf 2.虚拟主机配置:nginx/conf/extra/learn.weixin.com.conf 配置完后,重启服务器!
- Nginx下配置虚拟主机的三种方法
Nginx下,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的 ...
- 通过ngx-lua来统计Nginx上的虚拟主机性能数据
Web server调研分析 Filed under: Web Server — cmpan @ 2012-10-29 20:38:34 摘要 简单可依赖的架构首先需要有一个简单可依赖的前端WebSe ...
- Centos7 nginx配置多虚拟主机过程
一.前提准备 1.已经安装好了的Centos7服务器 2.ip 为192.168.1.209 [本次的配置ip] 3.确定防火墙等已经关闭 二.nignx配置文件参数详解 要配置多台虚拟主机,就需 ...
- Nginx如何配置虚拟主机?
注意,该环境是依赖于http://www.php20.com/forum.php?m ... &extra=page%3D1 基础上进行配置.默认不具备这些文件 .下面是增加一个mytest点 ...
- nginx下配置虚拟主机
linux 虚拟机下配置虚拟主机 nginx.conf 文件不动, 在 conf.d 或者 conf 目录下 新建项目.conf server { listen 80; server_name loc ...
随机推荐
- [原]CentOS 7.2 1511部署L2TP/IPsec服务器及客户端
快过年了,感觉从去年开始,我们公司就变成了“别人的公司”,基本上提前一星期就放假了,好开心.正好可以利用这一段时间,把前段时间一些疑惑的问题解决下:) 然而挡在面前的一个拦路虎是:很多时候不能愉快的G ...
- centos7 微信安装
安装过程如下: ,下载最新版本tar.gz压缩包 wget https://github.com/geeeeeeeeek/electronic-wechat/releases/download/V2. ...
- 腾讯云的基本配置(centos 7.1)及mysql的使用
因为想在微信上开发些东西,所以租用了一个月的腾讯云. 推荐选择的镜像是centos7.1.这个系统的选择和本地操作系统基本没有关系. 首先要登录到云主机中,用户名是root,密码是当初自己设置的那一个 ...
- java中random的几个方法的使用Math.random()和random().
random java中我们有时候也需要使用使用random来产生随机数,下面我来简单的介绍下java中random的使用方法 第一种:Math.random() public static doub ...
- MySQL安装及后续配置
rpm -qa | grep mysql 检查已安装的mysql版本 rpm -e --nodeps mysql-libs-5.1.71 卸载 tar -zxvf MySQL.tar.gz 解压 安 ...
- 连接hive
bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & ...
- Codeforces Round #486 (Div. 3) D. Points and Powers of Two
Codeforces Round #486 (Div. 3) D. Points and Powers of Two 题目连接: http://codeforces.com/group/T0ITBvo ...
- Paper/ Overview | CNN(未完待续)
目录 I. 基础知识 II. 早期尝试 1. Neocognitron, 1980 2. LeCun, 1989 A. 概况 B. Feature maps & Weight sharing ...
- 深度搜索优先(全排列)//本内容来自《啊哈!算法》或者英文名《Aha!Algorithms》)
package Mypackage; import java.util.Scanner; public class 全排列{ static int a[]=new int[10]; static ...
- 检索COM类工厂中CLSID为{10020100-E260-11CF-AE68-00AA004A34D5}的组件时失败,原因是出现以下错误:80040154
{"检索 COM 类工厂中 CLSID 为 {10020100-E260-11CF-AE68-00AA004A34D5} 的组件时失败,原因是出现以下错误: 80040154."} ...