Nginx反向代理多虚拟主机代理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx反向代理多虚拟主机代理相关的知识,希望对你有一定的参考价值。
根据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 *:80 //设置虚拟主机监听地址 <VirtualHost *:80> 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 *:80> 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 *:80 <VirtualHost *:80> 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 *:80> 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 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_pools { server 192.168.119.130:80 weight=5; server 192.168.119.133:80 weight=5; server 192.168.119.131:80 weight=5 backup; } server { listen 80; 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:80 0.0.0.0:* LISTEN 2058/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反向代理多虚拟主机代理的主要内容,如果未能解决你的问题,请参考以下文章