zoukankan      html  css  js  c++  java
  • 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/>

     返回结果正常

  • 相关阅读:
    微软的权限框架Asp.Net Identity
    排序算法
    在线编辑器
    It's only too late if you decide it is. Get busy living, or get busy dying(转)
    一个程序员的四年经历反思(转)
    wamp的安装使用(转)
    JDBC连接数据库经验技巧(转)
    重写ResultSet实现分页功能(最好的分页技术)(转)
    import android.provider.Telephony cannot be resolved
    linux-多线程
  • 原文地址:https://www.cnblogs.com/hwlong/p/6036023.html
Copyright © 2011-2022 走看看