nginx负载均衡fair模块安装和配置

yum安装的nginx是不带nginx-upstream-fair模块的,所以在负载均衡的策略中使用fair会报错。

1、安装nginx的依赖包

yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

2、查看原nginx版本信息

#/usr/sbin/nginx 默认yun安装的nginx执行行文路径
#-V大写的v是详细的版本和配置信息

/usr/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

3、 去下载nginx和fair
去nginx官网找到对应的nginx版本,我这里是1.18.0,所以直接下载
http://nginx.org/download/nginx-1.18.0.tar.gz
去nginx源码模块https://www.nginx.com/resources/wiki/modules/fair_balancer/下载nginx-upstream-fair模块。
将下载的nginx和fair模块解压到本地目录。这里直接放到/home目录下了

[root@localhost home]# pwd
/home
[root@localhost home]# ll
total 1028
-rw-r--r--. 1 root root   10073 Dec 19 13:23 gnosek-nginx-upstream-fair-a18b409.tar.gz
drwxr-xr-x. 9 1001 1001     186 Dec 19 13:29 nginx-1.18.0
-rw-r--r--. 1 root root 1039530 Dec 19 13:21 nginx-1.18.0.tar.gz
drwxrwxr-x. 2 root root      89 Apr  8  2012 nginx-upstream-fair

4、配置fair模块
进入nginx源码目录,使用前面第2步查出来的配置信息configure arguments:,在后面添加–add-module=/home/nginx-upstream-fair。这里是你解压的nginx-upstream-fair路径

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/home/nginx-upstream-fair

5、编译
直接使用make编译,不要加make install。后面手动去替换就好。

如果出现错误: ngx_http_upstream_srv_conf_t’ has no member named ‘default_port

/home/nginx-upstream-fair/ngx_http_upstream_fair_module.c: In function ‘ngx_http_upstream_init_fair_rr’:
/home/nginx-upstream-fair/ngx_http_upstream_fair_module.c:543:28: error: ‘ngx_http_upstream_srv_conf_t’ has no member named ‘default_port’
     if (us->port == 0 && us->default_port == 0) {
                            ^
/home/nginx-upstream-fair/ngx_http_upstream_fair_module.c:553:51: error: ‘ngx_http_upstream_srv_conf_t’ has no member named ‘default_port’
     u.port = (in_port_t) (us->port ? us->port : us->default_port);

找到nginx的源码 src/http/ngx_http_upstream.h文件找到ngx_http_upstream_srv_conf_s结构添加in_port_t default_port;

 struct ngx_http_upstream_srv_conf_s {
    ngx_http_upstream_peer_t         peer;
    void                           **srv_conf;

    ngx_array_t                     *servers;  /* ngx_http_upstream_server_t */

    ngx_uint_t                       flags;
    ngx_str_t                        host;
    u_char                          *file_name;
    ngx_uint_t                       line;
    in_port_t                        port;
    in_port_t                        default_port;
    ngx_uint_t                       no_port;  /* unsigned no_port:1 */

#if (NGX_HTTP_UPSTREAM_ZONE)
    ngx_shm_zone_t                  *shm_zone;
#endif
};

修改保存后,再回到之前继续make
6、用新编译的nginx文件覆盖之前的
编译好的文件会在nginx源码目录中的objs下。

mv /usr/sbin/nginx /usr/sbin/nginx_bak  # 备份原来的
cp /home/nginx-1.18.0/objs/nginx /usr/sbin/ginx
systemctl start nginx
上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 沪ICP备2022009235号 沪公网安备31011802005110号 青浦区尚茂路798弄 联系方式-13697081366