版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kwu_ganymede/article/details/73694478
Nginx安装与负载均衡配置
1、CentOS下需要的依赖包
yum install gcc-c++ -y
yum install pcre pcre-devel -y
yum install zlib zlib-devel -y
yum install openssl openssl-devel -y
2、下载nginx源码
wget http://nginx.org/download/nginx-1.13.1.tar.gz
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/08a395c66e42.zip
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
解压三个文件可得到
nginx-1.13.1
nginx-goodies-nginx-sticky
nginx_upstream_check_module-master
3、编译并安装nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=../nginx-goodies-nginx-sticky --add-module=../nginx_upstream_check_module-master
指定安装的目录,及依赖的组件源码
编译时出现异常
ngx_http_sticky_misc.c: In function 「ngx_http_sticky_misc_md5」:
ngx_http_sticky_misc.c:152:15: ERROR:「MD5_DIGEST_LENGTH」 undeclared (first use in this function)
u_char hash[MD5_DIGEST_LENGTH];
解决方式就是修改在你下载解压缩之后的sticky模块文件夹中的ngx_http_sticky_misc.c文件
将这两个模块 <openssl/sha.h> and <openssl/md5.h>包含到文件ngx_http_sticky_misc.c
#include <nginx.h>
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include <ngx_sha1.h>
#include <openssl/sha.h>
#include <openssl/md5.h>
#include "ngx_http_sticky_misc.h"
安装
make && make install
4、启动nginx
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin -s stop
5、负载均衡的配置
vim conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';
server
{
listen 80;
server_name hadoop4.ganymede.com;
index index.jsp index.html index.htm;
location /mdm
{
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://mdm_balance;
}
access_log /data/logs/nginx/a_access.log main;
error_log /data/logs/nginx/a_error.log;
}
upstream mdm_balance{
sticky;
server hadoop4:8086 weight=1 max_fails=2 fail_timeout=150s;
server hadoop5:8086 weight=2 max_fails=2 fail_timeout=150s;
}
}
6、修改配置后reload服务
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload -c /usr/local/nginx/conf/nginx.conf