Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分流插件 cookie 获取信息无效 #406

Open
sydowma opened this issue Jan 6, 2020 · 10 comments
Open

分流插件 cookie 获取信息无效 #406

sydowma opened this issue Jan 6, 2020 · 10 comments

Comments

@sydowma
Copy link
Contributor

sydowma commented Jan 6, 2020

使用的Orange版本

v0.8.1

需求或场景

使用分流插件按照 Cookie 里的值进行分流,发现获取不到数据

使用的插件

分流插件

具体的配置

image

    upstream baidu {
        server baidu.com;
    }
➜  GitHub curl --cookie "env=test1" www.baidu.com
Host: www.baidu.com  URI: /
[Query Params]{}
[Post Params]{}
[Header Params]{"host":"www.baidu.com","x-forwarded-scheme":"http","x-forwarded-for":"124.250.97.124","x-real-ip":"124.250.97.124","connection":"close","cookie":"env=test1","accept":"*\/*","user-agent":"curl\/7.64.1"}
124.250.97.124 - - [06/Jan/2020:15:55:40 +0800] "GET / HTTP/1.1" 200 279 "-" "curl/7.64.1" "0.001" "-" "-" "-""127.0.0.1:8001" "200" "279" "0.000"
期望的结果
错误的结果
2020/01/06 15:48:23 [error] 21400#21400: *75 [lua] condition.lua:10: judge(): assert_condition error: nil = test1, client: 124.xxx.xx.xxx, server: , request: "GET / HTTP/1.1", host: "www.baidu.com"
2020/01/06 15:48:23 [error] 21400#21400: *75 connect() failed (111: Connection refused) while connecting to upstream, client: 124.xxx.xx.xxx, server: , request: "GET / HTTP/1.1", upstream: "http://[::1]:8001/", host: "www.baidu.com"
@zhangbao0325
Copy link
Collaborator

麻烦贴一下你的divide插件最终生成的json配置信息以及你的nginx配置文件?

@sydowma
Copy link
Contributor Author

sydowma commented Jan 6, 2020

worker_processes auto;
user root;

events {
    worker_connections  4096;
}

# optional: path of orange.conf
env ORANGE_CONF;

http {
    resolver 114.114.114.114; # replace it with your favorite config
    charset UTF-8;
    include ./mime.types;

    log_format  main '$remote_addr - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$request_time" "$ssl_protocol" "$ssl_cipher" "$http_x_forwarded_for"'
    '"$upstream_addr" "$upstream_status" "$upstream_response_length" "$upstream_response_time"';

    access_log  ./logs/access.log  main;
    error_log ./logs/error.log debug;

    sendfile        on;
    keepalive_timeout  65;

    upstream default_upstream {
        server localhost:8001;
    }
    upstream baidu {
        server baidu.com;
    }
    #
    # Config client_body_buffer_size equal client_max_body_size for enforcing in-memory buffering of the whole request body
    # ref: https://github.com/openresty/lua-nginx-module/issues/521
    #
    # official instruct docs http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size
    #
    client_body_buffer_size 1m;
    client_max_body_size 1m;

    #----------------------------Orange configuration-----------------------------
    lua_package_path  "$prefix/deps/share/lua/5.1/?.lua;$prefix/deps/share/lua/5.1/orange/?.lua;$prefix/?.lua;/usr/share/lua/5.1/?.lua;/usr/local/lor/?.lua;;";
    lua_package_cpath "$prefix/deps/lib64/lua/5.1/?.so;$prefix/deps/lib/lua/5.1/?.so;/usr/lib64/lua/5.1/?.so;;";
    lua_code_cache on;

    lua_shared_dict orange_data 20m; # should not removed. used for orange data, e.g. plugins configurations..

    lua_shared_dict status 1m; # used for global statistic, see plugin: stat
    lua_shared_dict waf_status 1m; # used for waf statistic, see plugin: waf
    lua_shared_dict monitor 10m; # used for url monitor statistic, see plugin: monitor
    lua_shared_dict rate_limit 10m; # used for rate limiting count, see plugin: rate_limiting
    lua_shared_dict property_rate_limiting 10m; # used for rate limiting count, see plugin: rate_limiting



    init_by_lua_block {
        local orange = require("orange.orange")
        local env_orange_conf = os.getenv("ORANGE_CONF")
        print(string.char(27) .. "[34m" .. "[INFO]" .. string.char(27).. "[0m", [[the env[ORANGE_CONF] is ]], env_orange_conf)

        -- Here, you can also use the absolute path, eg: local confige_file = "/home/openresty/orange/conf/orange.conf"
        local config_file = env_orange_conf or ngx.config.prefix().. "/conf/orange.conf"
        local config, store = orange.init({
            config = config_file
        })

        -- the orange context
        context = {
            orange = orange,
            store = store,
            config = config
        }
    }

    init_worker_by_lua_block {
        local orange = context.orange
        orange.init_worker()
    }

    # main server
    server {
        listen       80;
        #server_name  my_domain.com;

        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        location / {
            set $upstream_host $host;
            set $upstream_url 'http://default_upstream';

            rewrite_by_lua_block {
                local orange = context.orange
                orange.redirect()
                orange.rewrite()
            }

            access_by_lua_block {
                local orange = context.orange
                orange.access()
            }

            # proxy
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Scheme $scheme;
            proxy_set_header Host $upstream_host;
            proxy_pass $upstream_url;


            header_filter_by_lua_block {
                local orange = context.orange
                orange.header_filter()
            }

            body_filter_by_lua_block {
                local orange = context.orange
                orange.body_filter()
            }

            log_by_lua_block {
                local orange = context.orange
                orange.log()
            }
        }

        location /robots.txt {
            return 200 'User-agent: *\nDisallow: /';
        }
    }

    # default upstream server
    server {
        listen 8001;
        server_name localhost 127.0.0.1;
        access_log ./logs/default_upstream_access.log main;
        error_log ./logs/default_upstream_error.log;

        location / {
            content_by_lua_block {
                local json = require "cjson"
                ngx.status = 200
                ngx.say("Host: "..ngx.var.host .. "  URI: " .. ngx.var.uri)
                ngx.say('[Query Params]', json.encode(ngx.req.get_uri_args()))
                ngx.req.read_body()
                ngx.say('[Post Params]', json.encode(ngx.req.get_post_args()))
                ngx.say('[Header Params]', json.encode(ngx.req.get_headers()))
            }
        }
    }


    # orange dashboard server
    server {
        listen       9999;
        stub_status on;
        #server_name  localhost;
        access_log ./logs/dashboard_access.log main;
        error_log ./logs/dashboard_error.log info;

        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        location /robots.txt {
            return 200 'User-agent: *\nDisallow: /';
        }

        # dashboard的静态文件
        location ~* /static/(.*) {
            alias ./dashboard/static/$1;
        }

        location / {
            set $template_root '';
            content_by_lua_block {
                context.views_path = ngx.config.prefix() .. "/dashboard/views"
                local main = require("dashboard.main")
                main:run()
            }
        }
    }

    # api server
    server {
        listen       7777;
        #server_name  localhost;
        access_log ./logs/api_access.log main;
        error_log ./logs/api_error.log info;

        location = /favicon.ico {
            log_not_found off;
            access_log off;
        }

        location /robots.txt {
            return 200 'User-agent: *\nDisallow: /';
        }

        location / {
            content_by_lua_block {
                local main = require("api.main")
                main:run()
            }
        }
    }

}

@sydowma
Copy link
Contributor Author

sydowma commented Jan 6, 2020

{
  "divide.enable": true,
  "divide.selectors": {
    "e4ed6bee-4666-47d0-a780-1a6c1a648ad8": {
      "time": "2020-01-02 16:13:48",
      "enable": true,
      "rules": [
        "e24e601b-5f1e-46a1-ae16-0a28afddf267"
      ],
      "id": "e4ed6bee-4666-47d0-a780-1a6c1a648ad8",
      "judge": [],
      "name": "hello",
      "handle": {
        "continue": false,
        "log": true
      },
      "type": 0
    }
  },
  "divide.selector.e4ed6bee-4666-47d0-a780-1a6c1a648ad8.rules": [
    {
      "id": "e24e601b-5f1e-46a1-ae16-0a28afddf267",
      "log": true,
      "name": "hello1",
      "upstream_host": "",
      "upstream_url": "http://baidu",
      "judge": {
        "type": 0,
        "conditions": [
          {
            "value": "test1",
            "name": "env",
            "operator": "=",
            "type": "Cookie"
          }
        ]
      },
      "enable": true,
      "time": "2020-01-06 16:00:08",
      "extractor": {
        "extractions": [],
        "type": 2
      }
    }
  ],
  "divide.meta": {
    "selectors": [
      "e4ed6bee-4666-47d0-a780-1a6c1a648ad8"
    ]
  }
}

@zhangbao0325
Copy link
Collaborator

配置没看出啥问题,可能是你curl的方式不对
你试试以下两种方式:
curl www.baidu.com -x 127.0.0.1:80 --cookie "env=test1"
curl www.baidu.com -x 127.0.0.1:80 -H 'Cookie:env=test1"'
另外,你的OR是那个版本?

@sydowma
Copy link
Contributor Author

sydowma commented Jan 7, 2020

配置没看出啥问题,可能是你curl的方式不对
你试试以下两种方式:
curl www.baidu.com -x 127.0.0.1:80 --cookie "env=test1"
curl www.baidu.com -x 127.0.0.1:80 -H 'Cookie:env=test1"'
另外,你的OR是那个版本?

v0.8 和 v0.8.1都试过,不行

@sydowma
Copy link
Contributor Author

sydowma commented Jan 7, 2020

image
我分别设置 Header 获取和 Cookie ,Header 是可以分流的,Cookie获取不到

@769344359
Copy link
Contributor

769344359 commented Jan 8, 2020

            access_by_lua_block {
                local orange = context.orange
                orange.access()
            }

添加一行orange.init_cookies()
改成

            access_by_lua_block {
                local orange = context.orange
                orange.init_cookies()      
                orange.access()
            }

@magaofei

@sydowma
Copy link
Contributor Author

sydowma commented Jan 9, 2020

            access_by_lua_block {
                local orange = context.orange
                orange.access()
            }

添加一行orange.init_cookies()
改成

            access_by_lua_block {
                local orange = context.orange
                orange.init_cookies()      
                orange.access()
            }

@magaofei

@769344359 可以了,感谢,不过这一行为什么没有出现的官方的 nginx.conf

@sydowma sydowma mentioned this issue Jan 9, 2020
@769344359
Copy link
Contributor

好像是cookie模块没有初始化 @magaofei

@sydowma
Copy link
Contributor Author

sydowma commented Jan 9, 2020

好像是cookie模块没有初始化 @magaofei

@769344359 那这一行是必须的吧?我提交了一个PR,添加上去了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants