根据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. 捉襟见肘之UITableViewCell重用引发的问题

    我记录一下自己如何解决cell内容重叠的问题 首先,复习一下:http://blog.csdn.net/omegayy/article/details/7356823 UITableViewCell的 ...

  2. CentOS下vsftp安装、配置、卸载(转载)

    看过多个教程,只有这个能完整成功配置: http://www.centoscn.com/image-text/config/2015/0122/4543.html

  3. springMVC的注解@RequestParam与@PathVariable的区别

    1.在SpringMVC后台控制层获取参数的方式主要有两种, 一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取. ...

  4. AppleHDA 10.9.3 disassm 1

    1.通过AppleHDAFunctionGroupFactory::createAppleHDAFunctionGroup(DevIdStruct *)实际创建相应的 AppleHDAFunction ...

  5. c语言程序

    汇编语言嵌入到c语言中 #include<stdio.h> int main(void) { int a,b,c; a=4; b=5; _asm { mov eax,a; add eax, ...

  6. C#MVC路由配置 之 动态请求伪装静态Json来欺骗CND

    public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.Ignore ...

  7. php分页的实现

    php常见分页有3种,一种是文字分页,比如说上一页,下一页之类的,还有一种数字分页,像1,2,3...之类的,这个比较简单,前者稍微复杂些.最后一种是他们2者结合. 下面是代码:    1 //获取当 ...

  8. bash: ifconfig: command not found解决方法

    1.问题: #ifconfig bash: ifconfig: command not found 2.原因:非root用户的path中没有/sbin/ifconfig ,其它的命令也可以出现这种情况 ...

  9. 为你的Visual Studio单独设置代理服务器

    http://blog.sina.com.cn/s/blog_58c506600101tycn.html 最近,因为国内访问Visual Studio Online(微软的免费代码托管服务,以前叫Te ...

  10. margin-bottom在IE6和IE7下失效的解决办法

    IE6/7下margin-bottom无效一般出现在容器里某元素设置后在父容器内无效,这个时候只需要在父容器中加入以下两句css,基本上所有的浏览器都兼容了: overflow:hidden;zoom ...