根据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. ubuntu 设置hostname

    永久修改hostname: # sudo vim /etc/hostname # sudo vim /etc/hosts

  2. HD1269迷宫城堡(有向图 && 划分连通块)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. Objective-C学习笔记之for( int )机制

    NSArray *myArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4&q ...

  4. gitlab迁移库地址后远程的切换

    本地需要把origin删除,然后再添加新的origin git remote rm origingit remote add origin [url] git push --set-upstream ...

  5. php装饰器模式完成文章编辑

    <?php //文章父类 class BaseArt{ protected $content; protected $art; public function __construct($cont ...

  6. ORACLE函数大全

    SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ...

  7. React入门简单实践

    参考文献: 1.React入门示例教程——阮一峰 2.React仅仅只是你的界限 React主要的优点就是增量更新(虚拟DOM)和组件化(状态机). <!DOCTYPE html> < ...

  8. thinkphp 语言包丢失

    Thinkphp 语言包丢失 造成的原因有那些 1.复制模板 预览时内容出现英文状态 如:show.html 解决:找到lang ,在zh-cn 复制想对应的文件包 改下名称就有可以 如:admin_ ...

  9. SimPholders Xcode快速访问沙盒

    SimPholders

  10. GoLang之基础

    GoLang之基础 Go是一种并发的.带垃圾回收的.快速编译的语言. 经典的"hello world"入门: package main import "fmt" ...