9

Nginx下lua根据客户端ip进行分发

 1 year ago
source link: https://bajie.dev/posts/20240123-lua_ip_forward/
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.
neoserver,ios ssh client

Nginx下lua根据客户端ip进行分发

2024-01-23 1 分钟阅读

公司的线上环境分为预发布和正式两个部分。

其实两个部分是公用一个Nginx前端的。

这样怎么分发呢?

lua即可,如果是公司来的某个固定ip,则分发到预发布,如果不是,就走正式环境。这样测试就简单多了,运维统一设置一个无线wifi,接入这个wifi就走某个固定ip,到的全是预发布环境,不用这个wifi就走正式环境,非常方便测试。

用lua进行分发:

location / {  
    content_by_lua '
            myIP = ngx.req.get_headers()["X-Real-IP"]
            if myIP == nil then
                myIP = ngx.req.get_headers()["x_forwarded_for"]
            end
            if myIP == nil then
                myIP = ngx.var.remote_addr
            end
            if myIP == "公司出口IP" then
                ngx.exec("@client")
            else
                ngx.exec("@client_test")
            end
        ';
} 

location @client{  
    proxy_next_upstream     error timeout;
    proxy_redirect          off;
    proxy_set_header        Host $host;
    #proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Real-IP $http_x_forwarded_for;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    100m;
    client_body_buffer_size 256k;
    proxy_connect_timeout   180;
    proxy_send_timeout      180;
    proxy_read_timeout      180;
    proxy_buffer_size       8k;
    proxy_buffers           8 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_pass http://client;

}
location @client_test{  
    proxy_next_upstream     error timeout;
    proxy_redirect          off;
    proxy_set_header        Host $host;
    #proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Real-IP $http_x_forwarded_for;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    client_max_body_size    100m;
    client_body_buffer_size 256k;
    proxy_connect_timeout   180;
    proxy_send_timeout      180;
    proxy_read_timeout      180;
    proxy_buffer_size       8k;
    proxy_buffers           8 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_pass http://client_test;
} 

Recommend

  • 87

    上一篇文章我们讲到了一个比较典型的PXE客户端无法找到操作系统映像的故障,今天再和大家一起分享一个关于 PXE客户端无法加载分发点映像的问题。具体的报错截图如下:从报错中我们可以看到,PXE客户端已经成功的找到了SCCM服务器,并且成功的加载了启动映像boot im...

  • 12
    • www.cnblogs.com 3 years ago
    • Cache

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

    敢于对过去告一个段落,才有信心掀开新的篇章! 关注:架构设计、领域驱动、统一认证、微服务、分布式、容器编排、持续集成、自动化测试、设计模式 ...

  • 18

    Measuring Nginx Cache Performance using Lua and Redis February 23, 2016 22:15 / lua

  • 6
    • wakzz.cn 3 years ago
    • Cache

    Nginx+Lua环境搭建

    Nginx+Lua环境搭建 祈雨的博客 2018-12-15

  • 8
    • wakzz.cn 3 years ago
    • Cache

    Nginx+Lua实现动态黑名单

    通过nginx+lua+redis可以实现nginx动态从redis读取需要拒绝的ip黑名单列表,并拒绝黑名单ip的访问请求。 其中redis中的ip黑名单列表既可以人工后台手动添加,也可以用类似logstash+elasticsearch的组合,实现logstash实时读取nginx的访问日志acces...

  • 8
    • wakzz.cn 3 years ago
    • Cache

    Nginx+Lua实现分布式缓存

    Nginx+Lua实现分布式缓存 祈雨的博客 2018-12-11

  • 3
    • wangxuemin.github.io 3 years ago
    • Cache

    nginx_lua 扩展让 nginx 拥有可编程能力

    nginx_lua 扩展让 nginx 拥有可编程能力 发表于...

  • 4
    • wulijun.github.io 3 years ago
    • Cache

    [译]Nginx和Lua

    [译]Nginx和Lua 04 Sep 2012 By 风之缘 这几个月里,我们逐步把Lua集成到Mixlr的前端Nginx配置中。 Lua是一个可以嵌入到Nginx配置文件中的动态脚本语言,...

  • 11
    • panda843.github.io 3 years ago
    • Cache

    Nginx+Lua实现访问日志收集系统

    Nginx+Lua实现访问日志收集系统 | 是潘达呀Nginx+Lua实现访问日志收集系统 发表于 2017-12-28 | 分类于 开发 | | 浏览4 次 | 字数统计: 1.4k | 阅读时长 ≈ 6网站数据统...

  • 14

    lua-nginx-module的所有指令以及所有方法 2022-10-30 17:13:00 http://www....

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK