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/>

 

 返回結果正常

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM