nginx负载均衡-基于多域名的负载均衡(二)

注意:

(1) 做负载均衡技术至少需要三台服务器:一台独立的负载均衡器,两台web服务器做集群

(2) keeplived(高可用)+nginx(负载均衡) 可以实现多域名对应一个VIP,并且访问不同域名,显示不同主页,可行,已测

一、nginx分别代理后端web1 和 web2的三台虚拟主机

1、web1(nginx-10.0.0.7)配置基于域名的虚拟主机(以下配置了3个虚拟主机)

复制代码
[root@Oldboy extra]# cat www.conf 
server {
        listen        80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;              #这个地方指向nginx安装目录的html/www/目录下
            index  index.html index.htm;
        }
}
[root@Oldboy extra]# cat bbs.conf 
server {
        listen        80;
        server_name  bbs.etiantian.org; 
        location / {
            root   html/bbs;
            index  index.php  index.html index.htm;
        }
}
[root@Oldboy extra]# cat blog.conf 
server {
        listen        80;
        server_name  blog.etiantian.org;
        location / {
        root     html/blog;
        index    index.php;
}

        location ~ .*\.(php|php5)?$ {
        root html/blog;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
        }
}

2、web2(apache-10.0.0.8)配置基于域名的虚拟主机(以下配置了3个虚拟主机)

复制代码
[root@Oldboy extra]# egrep -v "#|^$" httpd-vhosts.conf 

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin oldboy@oldboyedu.com
    DocumentRoot "/application/apache2.2.31/htdocs/www"
    ServerName   www.etiantian.org
    ServerAlias etiantian.org
    ErrorLog "/app/logs/www-error_log"
    CustomLog "/app/logs/www-access_log" common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin oldboy@oldboyedu.com
    DocumentRoot "/application/apache2.2.31/htdocs/bbs"
    ServerName   bbs.etiantian.org
    ErrorLog "/app/logs/bbs-error_log"
    CustomLog "/app/logs/bbs-access_log" common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin oldboy@oldboyedu.com
    DocumentRoot "/application/apache2.2.31/htdocs/blog"
    ServerName   blog.etiantian.org
    ErrorLog "/app/logs/blog-error_log"
    CustomLog "/app/logs/blog-access_log" common
</VirtualHost>

3、负载均衡器(LB-nginx-10.0.0.6)配置反向代理

复制代码
[root@Oldboy conf]# cat 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_pool {                                             地址池
    server 10.0.0.7:80  weight=1;
    server 10.0.0.8:80  weight=1;
}




server {
        listen       80;
        server_name  www.etiantian.org  bbs.etiantian.org  blog.etiantian.org;

        #ssl on;
    #ssl_certificate /usr/local/nginx/conf/33iq.crt;
    #ssl_certificate_key /usr/local/nginx/conf/33iq_nopass.key;

        location / {
            index  index.html index.htm;
            proxy_pass http://web_pool;                                                   反向代理到地址池
            proxy_set_header Host  $host;                                                将请求头信息也一起反代给后端的服务器
            proxy_set_header X-Forwarded-For $remote_addr;               将客户端的真实ip映射到后端的web服务器中
        }
    }


}

解释:
当客户端分别访问 www.etiantian.org 、 bbs.etiantian.org、 blog.etiantian.org 时,nginx会带着这些主机头请求后端web集群,web服务器中的虚拟主机会自动识别nginx请求的主机头,并回复nginx请求,nginx再将web端的回复,交给客户端。

也可以代理单台服务器:
location / {
            index  index.html index.htm;
            proxy_pass http://172.16.100.100;                                                   
            proxy_set_header Host  $host;                                                
            proxy_set_header X-Forwarded-For $remote_addr;           
        }

4、修改web端日志访问ip

复制代码
默认情况下web端日志记录的访问ip是负载均衡器的ip,所以需要修改为真实客户端ip
(1) 负载均衡器(LB)配置
server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            index  index.html index.htm;
            proxy_pass http://blog_pool;                                          代理blog动态页面
            proxy_set_header Host  $host;
            proxy_set_header X-Forwarded-For $remote_addr;      负载均衡器开启ip转发,将客户端ip映射到web服务器
        }
    }
(2) web1端修改日志格式来接收客户端的真实ip(nginx主配置文件)
cat  nginx.conf

log_format  main  '$http_x_forwarded_for  $remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';
(3) web2端修改日志格式来接收客户端的真实ip (apache主配置文件)
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b"  common
相关推荐
逸模6 小时前
告别熬夜手工整理台账,逸模智能归集实现项目数据自动化存档
大数据·运维·人工智能·笔记·其他·信息可视化·自动化
sbjdhjd6 小时前
Redis 主从复制、哨兵高可用与 Cluster 集群部署实验手册
运维·前端·redis·云原生·开源·bootstrap·html
AOwhisky7 小时前
MySQL 学习笔记(第四期):SQL 语言之多表查询
linux·运维·网络·数据库·笔记·学习·mysql
Phantom Void7 小时前
服务器处理客户端请求的设计方法
linux·运维·网络
倔强的石头1067 小时前
Fooocus开源神器+cpolarAI让绘画告别服务器依赖
运维·服务器·cpolar
wei_shuo7 小时前
服务器挂了等用户投诉才发现?我用Beszel搭了轻量监控系统,宕机第一时间通知我
运维·服务器
王码码20357 小时前
多台服务器怎么统一看状态?Beszel 轻量监控,搭起来不费事
运维·服务器·后端·安全·阿里云·接口·web
APItesterCris11 小时前
实战教程:借助 Open Claw + 淘宝商品 API,低成本实现电商自动化监控与智能选品
大数据·运维·自动化
风曦Kisaki12 小时前
# 自动化运维Day03:Ansible模块进阶(setup,debug),四种常用变量,进阶语法;Ansible Roles(角色)
运维·自动化·ansible
赵民勇12 小时前
Linux strings命令详解
linux·运维