根据http://www.cnblogs.com/zzzhfo/p/6032095.html这个环境配置

  • 在web01和web02上配置基于域名的虚拟主机

web01

[root@web01 /]# mkdir -p /var/www/www
[root@web01 /]# mkdir -p /var/www/bbs
[root@web01 /]# echo "<h1>bbs.test.com<h1/>" > /var/www/bbs/index.html
[root@web01 /]# echo "<h1>www.test.com<h1/>" > /var/www/www/index.html
[root@web01 /]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *: //设置虚拟主机监听地址
<VirtualHost *:>
DocumentRoot "/var/www/www"
ServerName www.test.com
ErrorLog "logs/www.test.com.error_log"
CustomLog "logs/www.test.com.access_log" common
</VirtualHost> <VirtualHost *:>
DocumentRoot "/var/www/bbs"
ServerName bbs.test.com
ErrorLog "logs/bbs.test.com.error_log"
CustomLog "logs/bbs.test.com.access_log" common
</VirtualHost>

web02 (可使用scp同步到web02)

[root@web02 /]# mkdir -p /var/www/www
[root@web02 /]# mkdir -p /var/www/bbs
[root@web02 /]# echo "<h1>bbs.test.com<h1/>" > /var/www/bbs/index.html
[root@web02 /]# echo "<h1>www.test.com<h1/>" > /var/www/www/index.html
[root@web02 /]# vim /etc/httpd/conf/httpd.conf
NameVirtualHost *:
<VirtualHost *:>
DocumentRoot "/var/www/www"
ServerName www.test.com
ErrorLog "logs/www.test.com.error_log"
CustomLog "logs/www.test.com.access_log" common
</VirtualHost> <VirtualHost *:>
DocumentRoot "/var/www/bbs"
ServerName bbs.test.com
ErrorLog "logs/bbs.test.com.error_log"
CustomLog "logs/bbs.test.com.access_log" common
</VirtualHost>

测试两台web的虚拟主机

[root@web_backup /]# vim /etc/hosts
192.168.119.130 www.test.com bbs.test.com
[root@web_backup /]# curl www.test.com
<h1>www.test.com<h1/>
[root@web_backup /]# curl bbs.test.com
<h1>bbs.test.com<h1/>
[root@web_backup /]# vim /etc/hosts
192.168.119.133 www.test.com bbs.test.com
[root@web_backup /]# curl www.test.com
<h1>www.test.com<h1/>
[root@web_backup /]# curl bbs.test.com
<h1>bbs.test.com<h1/>

测试:通过负载均衡器虚拟主机能否正常反问

修改/etc/hosts  地址改为Nginx负载均衡器的地址(lb)

[root@web_backup /]# vim /etc/hosts
192.168.119.128 www.test.com bbs.test.com
[root@web_backup /]# curl bbs.test.com
<h1>www.test.com<h1/>
[root@web_backup /]# curl www.test.com
<h1>www.test.com<h1/>

返回的结果都是第一个域名

通过 修改Nginx负载均衡器的配置文件/usr/local/nginx/conf/nginx.conf 在location 添加proxy_set_header Host $host;

[root@lb01 /]# vim /usr/local/nginx/conf/nginx.conf
worker_processes ;
events {
worker_connections ;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout ; upstream web_pools {
server 192.168.119.130: weight=;
server 192.168.119.133: weight=;
server 192.168.119.131: weight= backup;
} server {
listen ;
server_name www.test.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://web_pools;
proxy_set_header Host $host;
}
}
}

重启nginx服务

[root@lb01 /]# nginx -s stop
[root@lb01 /]# nginx
[root@lb01 /]# netstat -anpt | grep nginx
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx

测试

[root@web_backup /]# curl www.test.com
<h1>www.test.com<h1/>
[root@web_backup /]# curl www.test.com
<h1>www.test.com<h1/>
[root@web_backup /]# curl bbs.test.com
<h1>bbs.test.com<h1/>
[root@web_backup /]# curl bbs.test.com
<h1>bbs.test.com<h1/>
[root@web_backup /]# curl www.test.com
<h1>www.test.com<h1/>

返回结果正常

Nginx反向代理多虚拟主机代理的更多相关文章

  1. Nginx安装及配置虚拟主机

    nginx安装部分 依赖环境 yum -y install gcc zlib openssl-devel zlib-devel 1. 下载好下面两个包:nginx-1.8.1.tar.gz pcre- ...

  2. [原]生产环境下的nginx.conf配置文件(多虚拟主机)

    [原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...

  3. nginx之全局设置,location,虚拟主机,日志管理

    nginx之全局设置,location,虚拟主机,日志管理 worker_processes 1;//子进程,cpu数*核数 ****************全局设置************** ** ...

  4. Nginx 配置实例-配置虚拟主机

    Nginx 配置实例-配置虚拟主机 配置基于域名的虚拟主机 1. 配置域名为 aaa.domain.com 的虚拟主机 1.1 nginx 中虚拟主机的配置 1.2 相关目录及文件的创建 1.3 验证 ...

  5. lvs,nginx反向代理,虚拟主机

    LVS NAT 拓扑 client | | LVS | | ------------------- | | | RS1 RS2 RS3 地址规划如下 机器名称 ip配置 ip配置 备注信息 LVS 1 ...

  6. nginx笔记 安装nginx 配置 反向代理 多虚拟主机

    1,检测linux上是否 通过yum安装了nginxrpm   -qi  nginx 2.安装nginx之前的依赖包yum install gcc patch libffi-devel python- ...

  7. Nginx安装、配置虚拟主机、反向代理、负载均衡

    1.   nginx安装 下载nginx: 官方网站: http://nginx.org/ 使用的版本是1.8.0版本. Nginx提供的源码. 1.1. 要求的安装环境 1.需要安装gcc的环境.y ...

  8. Nginx(http协议代理 搭建虚拟主机 服务的反向代理 在反向代理中配置集群的负载均衡)

    Nginx 简介 Nginx (engine x) 是一个高性能的 HTTP 和反向代理服务.Nginx 是由伊戈尔·赛索耶夫为俄罗斯访问量第二的 Rambler.ru 站点(俄文:Рамблер)开 ...

  9. nginx常用配置系列-虚拟主机

    本来准备详尽的出一份nginx配置讲解,但nginx功能配置繁多,平常使用中使用最多的一般有: 1. 虚拟主机配置 2. HTTPS配置 3. 静态资源处理 4. 反向代理 ============= ...

随机推荐

  1. 【Phylab2.0】Alpha版本发布说明

    新版本特性 更多的实验 Part Phylab2.0[Alpha] Phylab1.0 1011 1010113 缺失 1011 1010212 缺失 1021 1020113 1021 102021 ...

  2. JS-节点属性(常用!)

    (开始很生疏,不想看的知识点,后来DOM中用的特别多,才发现很重要.还有几个点,是比较容易忽略的,值得注意!) <!DOCTYPE html><html> <head&g ...

  3. tomcat并发

    Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与硬件性能和CPU数量都有很大关系的.更好的硬件,更多的处理器都会使Tomcat支持更多的并发. Tomcat默认的HTTP实现是采用阻塞式 ...

  4. c#优化

    优化反射 http://kb.cnblogs.com/page/172119/ http://www.2cto.com/kf/201301/182765.html http://blog.sina.c ...

  5. SQLServer------解决IP地址登录不了数据库问题

    1.找到配置管理器,打开 2.关掉SQLExpress,打开MSSQLServer 3.配置MSSQLServer(启用Named Pipes和TCP/IP) 4.修改TCP/IP属性(端口:1433 ...

  6. Docker create image

    Dockerfile FROM java:8 MAINTAINER dudu ADD springts_1-0.0.1-SNAPSHOT.jar app.jar EXPOSE 8080 ENTRYPO ...

  7. phpexcel操作

    <?php include './PHPExcel/PHPExcel.php'; include './PHPExcel/PHPExcel/Writer/Excel2007.php'; //或者 ...

  8. JDK的安装和配置

    JDK8 是JDK的最新版本,加入了很多新特性,如果我们要使用,需要下载安装: JDK8在windows xp下安装有点问题,所以在WIN7下安装 WIN7操作系统有32位和64位,分别要下载对应的J ...

  9. 《深入理解bootstrap》读书笔记:第4章 CSS组件(下)

    十. 标签(.label类,label-xxx) 高亮一些标题部分. 1 2 3 4 5 6 <h1>HELLO<span class="label label-defau ...

  10. sqlmap的一些小技巧

    前言 很多人都使用sqlmap来进行SQL注入测试,但是很多人只是简简单单的current-user,current-db,-D,-T,--dump这样子来做,其实sqlmap还有很多很强大的功能,这 ...