nginx 编译安装sticky时报错处理

一般企事业单位的内网按照部门划分网段,ip hash 的负载均衡策略容易导致负载失衡,比如某个网段地址多,一些网段地址少,IP hash是基于IPv4地址的前三段来区分的(开发者可能觉得机器处理区分所有IP太累么?配置文件中让用户自己选择不好么?)。所以要使用sticy 模块来解决这个问题。不过使用nginx 1.25 和1.24等较新版本编译sticky模块时,make阶段报错:

  -o objs/addon/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.o \
        /opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c
/opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c: 在函数‘ngx_http_init_sticky_peer’中:
/opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:47: 错误:传递‘ngx_http_parse_multi_header_lines’的第 1 个参数时在不兼容的指针类型间转换 [-Werror=incompatible-pointer-types]
  207 |         if (ngx_http_parse_multi_header_lines(&r->headers_in.cookie, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
      |                                               ^~~~~~~~~~~~~~~~~~~~~
      |                                               |
      |                                               ngx_table_elt_t ** {或称 struct ngx_table_elt_s **}
In file included from /opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:
src/http/ngx_http.h:106:72: 附注:需要类型‘ngx_http_request_t *’ {或称 ‘struct ngx_http_request_s *’},但实参的类型为‘ngx_table_elt_t **’ {或称 ‘struct ngx_table_elt_s **’}
  106 | _table_elt_t *ngx_http_parse_multi_header_lines(ngx_http_request_t *r,
      |                                                 ~~~~~~~~~~~~~~~~~~~~^

/opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:70: 错误:传递‘ngx_http_parse_multi_header_lines’的第 2 个参数时在不兼容的指针类型间转换 [-Werror=incompatible-pointer-types]
  207 |  (ngx_http_parse_multi_header_lines(&r->headers_in.cookie, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
      |                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |                                                            |
      |                                                            ngx_str_t *

In file included from /opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:
src/http/ngx_http.h:107:22: 附注:需要类型‘ngx_table_elt_t *’ {或称 ‘struct ngx_table_elt_s *’},但实参的类型为‘ngx_str_t *’
  107 |     ngx_table_elt_t *headers, ngx_str_t *name, ngx_str_t *value);
      |     ~~~~~~~~~~~~~~~~~^~~~~~~
/opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:207:13: 错误:提供给函数‘ngx_http_parse_multi_header_lines’的实参太少
  207 |         if (ngx_http_parse_multi_header_lines(&r->headers_in.cookie, &iphp->sticky_conf->cookie_name, &route) != NGX_DECLINED) {
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.c:9:
src/http/ngx_http.h:106:18: 附注:在此声明
  106 | ngx_table_elt_t *ngx_http_parse_multi_header_lines(ngx_http_request_t *r,
      |                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1:所有的警告都被当作是错误
make[1]: *** [objs/Makefile:1594:objs/addon/nginx-goodies-nginx-sticky-module-ng-08a395c66e42/ngx_http_sticky_module.o] 错误 1
make[1]: 离开目录“/opt/nginx-1.24.0”
make: *** [Makefile:10:build] 错误 2
[root@localhost nginx-1.24.0]# ^C
[root@localhost nginx-1.24.0]# ^C

搞了半天,从网上也搜了不少文章介绍的方法都不行。 直到看到了这个文章:

nginx1.24版本编译sticky模块报‘ngx_http_headers_in_t’ has no member named ‘cookies’的解决方案_error: ‘ngx_http_headers_in_t’ has no member named-优快云博客icon-default.png?t=N7T8https://blog.youkuaiyun.com/qq_21076567/article/details/134922929主要两句话:

替换 sticky模块下ngx_http_sticky_module.c文件的文件内容:

 sed -i "s/ngx_http_parse_multi_header_lines.*/ngx_http_parse_multi_header_lines(r, r->headers_in.cookie, \&iphp->sticky_conf->cookie_name, \&route) != NULL){/g" ngx_http_sticky_module.c

实测1.24 和1.25的源码编译均有效。看起来好的解决办法就是简单粗暴。

Stickynginx的一个模块,它是基于cookie的一种nginx的负载均衡解决方案,通过分发和识别cookie,来使同一个客户端的求落在同一台服务器上,默认标识名为route (a)客户端首次发起访问求,nginx接收后,发现求头没有cookie,则以轮询方式将求分发给后端服务器。 (b)后端服务器处理求,将响应数据返回给nginx。 (c)此nginx生成带route的cookie,返回给客户端。route的值与后端服务器对应,可能是明文,也可能是md5、sha1等Hash值 (d)客户端接收求,并保存带route的cookie。 (e)当客户端下一次发送,会带上route,nginx根据接收到的cookie中的route值,转发给对应的后端服务器。 其他需要注意的 (a)同一客户端的求,有可能落在不同的后端服务器上。如果客户端启动发起多个求。由于这些求都没带cookie,所以服务器会随机选择后端服务器,返回不同的cookie。当这些求中的最后一个求返回,客户端的cookie才会稳定下来,值以最后返回的cookie为准。 (b)cookie不一定生效。由于cookie最初由服务器端下发,如果客户端禁用cookie,则cookie不会生效。 (c)客户端可能不带cookie。Android客户端发送,一般不会带上所有的cookie,需要明确指定哪些cookie会带上。如果希望用sticky做负载均衡,对Android开发说加上cookie。 (d)cookie名称不要和业务使用的cookie重名。Sticky默认的cookie名称是route,可以改成任何值。 (e)客户端发的第一个求是不带cookie的。服务器下发的cookie,在客户端下一次才能生效。 (f)Nginx sticky模块不能与ip_hash同使用 sticky [name=route] [domain=.foo.bar] [path=/] [expires=1h] [hash=index|md5|sha1] [no_fallback] [secure] [httponly]; [name=route] 设置用来记录会话的cookie名称 [domain=.foo.bar] 设置cookie作用的域名 [path=/] 设置cookie作用的URL路径,默认根目录 [expires=1h] 设置cookie的生存期,默认不设置,浏览器关闭即失效,需要是大于1秒的值 [hash=index|md5|sha1] 设置cookie中服务器的标识是用明文还是使用md5值,默认使用md5 [no_fallback] 设置该项,当sticky的后端机器挂了以后,nginx返回502 (Bad Gateway or Proxy Error) ,而不转发到其他服务器,不建议设置 [secure] 设置启用安全的cookie,需要HTTPS支持 [httponly] 允许cookie不通过JS泄漏,没用过
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值