nginx负载均衡-基于端口的负载均衡(一)

注意:

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

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

1、web1(nginx-10.0.0.7)配置基于端口的虚拟主机

复制代码
[root@Oldboy extra]# cat www.conf 
server {
        listen        10.0.0.7:80;
#     server_name  www.etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        access_log  logs/www_access.log  main;
}
[root@Oldboy extra]# cat bbs.conf 
server {
        listen       10.0.0.7:8080;
#     server_name  bbs.etiantian.org;
        location / {
            root   html/bbs;
            index  index.php  index.html index.htm;
        }
        access_log  logs/bbs_access.log  main;
}
[root@Oldboy extra]# cat blog.conf           动态页面
server {
        listen       10.0.0.7:8081;
#      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;
        }
        access_log  logs/blog_access.log  main;
}

2、web2(apache-10.0.0.8)配置基于端口的虚拟主机

复制代码
NameVirtualHost 10.0.0.8:80
Listen 8080                                           手动写上监听端口,80在主配置文件
Listen 8081
<VirtualHost *:80>
    ServerAdmin oldboy@oldboyedu.com
    DocumentRoot "/application/apache2.2.31/htdocs/www"
    ServerAlias etiantian.org
    ErrorLog "/app/logs/www-error_log"
    CustomLog "/app/logs/www-access_log" common
</VirtualHost>

<VirtualHost 10.0.0.8:8080>
    ServerAdmin oldboy@oldboyedu.com
    DocumentRoot "/application/apache2.2.31/htdocs/bbs"
    ErrorLog "/app/logs/bbs-error_log"
    CustomLog "/app/logs/bbs-access_log" common
</VirtualHost>

<VirtualHost 10.0.0.8:8081>                                                            动态页面
    ServerAdmin oldboy@oldboyedu.com
    DocumentRoot "/application/apache2.2.31/htdocs/blog"
    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 www_pool {                                                   www静态地址池
    server 10.0.0.7:80  weight=1;                                      权重越大优先级越高
    server 10.0.0.8:80  weight=1;
}

 upstream bbs_pool {                                                    bbs静态地址池
    server 10.0.0.7:8080  weight=1;
    server 10.0.0.8:8080  weight=1;
}

 upstream blog_pool {                                                  blog动态地址池
    server 10.0.0.7:8081  weight=1;
    server 10.0.0.8:8081  weight=1;
}



server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            index  index.html index.htm;
            proxy_pass http://www_pool;                                          代理www静态页面
            include proxy.conf;
        }
    }


server {
        listen       80;
        server_name  bbs.etiantian.org;
        location / {
            index  index.html index.htm;
            proxy_pass http://bbs_pool;                                            代理bbs静态页面
            include proxy.conf;
        }
    }

server {
        listen       80;
        server_name  blog.etiantian.org;
        location / {
            index  index.html index.htm;
            proxy_pass http://blog_pool;                                          代理blog动态页面
            include proxy.conf;
        }
    }
}

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端修改(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端修改 (apache主配置文件)
LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b"  common
相关推荐
苹果醋311 小时前
iview— Select— Option选中后有空格
运维·vue.js·spring boot·nginx·课程设计
微爱帮监所写信寄信13 小时前
微爱帮监狱写信寄信小程序针对互联网黑灰产攻击防护体系
小程序·负载均衡·信息与通信·安全架构·安全防护·监狱寄信
catoop15 小时前
网站安全加固:优化 Nginx 安全头配置
nginx·安全
驯龙高手_追风18 小时前
OpenResty+NtripCaster实现挂载点负载均衡使用初探
junit·负载均衡·openresty
GRsln19 小时前
解决微信小程序报“errno“:600001 ERR_CERT_AUTHORITY_INVALID问题
nginx·微信小程序·小程序·ssl
艾德金的溪20 小时前
性能数据接收接口I/O堵塞问题处理及Nginx分流扩容方案
nginx
汽车仪器仪表相关领域21 小时前
ZRT-I 精密减速器测试系统
大数据·运维·功能测试·安全·单元测试·负载均衡·压力测试
iru1 天前
nginx被报CVE-2025-1695漏洞,检查后反馈是误报
运维·nginx
cnskylee2 天前
【Nginx】Nginx-1.28.1版本已恢复对CentOS 7的兼容性
运维·nginx·centos
Knight_AL3 天前
Docker 中的端口映射原理:为什么 Nginx 要 `listen 80`,而不是“随便写端口”
nginx·docker·容器