4

nginx~按着客户端IP进行负载转发

 2 years ago
source link: https://www.cnblogs.com/lori/p/15402803.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
关注:架构设计、领域驱动、统一认证、微服务、分布式、容器编排、持续集成、自动化测试、设计模式
大叔系列: keycloak文章汇总 | k8s学习笔记 | Java&Jvm | Springcloud | Springboot | Lind.DDD领域驱动架构 | DotNetCore | Docker学习笔记 | 设计模式 | 重构改善即有代码的设计

nginx~按着客户端IP进行负载转发

客户端IP地址我们在nginx里可以通过$remote_addr来获取,如果我们在上新功能时,希望新功能先通过本公司内部测试,测试通过后再由所有用户测试,这时就用到了灰度发布,而对于灰度的实现上,你的请求转发是一个重要内容.

  • nginx通过客户端ip进行转发代码
upstream keycloak1 {
    server 192.168.3.181:8080;
}
upstream keycloak2 {
    server 192.168.3.181:9090;
}

server {
    listen   81;
    location / {
        proxy_ignore_client_abort on;
        proxy_read_timeout 200s;
        proxy_redirect    off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        client_max_body_size    1000m;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        if ($remote_addr ~ "192.168.4.138")
        {
            proxy_pass    http://keycloak2;
            break;
        }
        proxy_pass    http://keycloak1;
    }
    
}

另外,可以看这篇文章https://blog.csdn.net/weixin_33938733/article/details/92048497来实现通过地址位置进行转发的功能。

作者:仓储大叔,张占岭,
荣誉:微软MVP
QQ:853066980

支付宝扫一扫,为大叔打赏!
o_IMG_7144.JPG

posted @ 2021-10-13 16:28  张占岭  阅读(13)  评论(0)  编辑  收藏  举报
努力加载评论中...

Copyright © 2021 张占岭
Powered by .NET 6 on Kubernetes


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK