安装nginx_upstream_check_module模块
rpm -ivh patch-2.7.1-12.el7_7.x86_64.rpm --force --nodeps
cd /app/nginx/nginx-1.17.3
修改/tmp/目录
patch -p1 < /tmp/nginx_upstream_check_module-master/check_1.16.1+.patch
配置Nginx
./configure --prefix=/app/nginx --add-module=/tmp/nginx_upstream_check_module-master/ --with-http_stub_status_module --with-http_ssl_module --with-pcre
编译安装
make && make install
安装sticky模块
unzip nginx-sticky.zip
cd /app/nginx/nginx-1.17.3
配置Nginx
./configure --prefix=/app/nginx --add-module=/tmp/nginx_upstream_check_module-master/ --add-module=/tmp/nginx-sticky/ --with-http_stub_status_module --with-http_ssl_module --with-pcre
编译安装
make
拷贝编译出来的二进制文件到nginx的sbin目录
/bin/cp -rf ./objs/nginx /app/nginx/sbin/nginx
Nginx.conf配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
server_tokens off;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream web {
sticky;
//按实际配置两套应用的IP和port
server IP:port weight=1;
server IP:port weight=2;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx http_4xx ;
}
server {
//按实际配置port
listen port ssl;
server_name localhost;
ssl_certificate XXXserver.crt;
ssl_certificate_key XXXserver.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
//按实际配置应用目录
location ^~/XXX {
alias /app/nginx_project/XXX;
index index.html index.htm;
}
//按实际配置应用目录
location ^~/XXXX {
client_max_body_size 1024m;
client_body_buffer_size 5m;
proxy_connect_timeout 3600s;
proxy_send_timeout 3600s;
proxy_read_timeout 3600s;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://web/XXXX;
# proxy_next_upstream error timeout http_500 http_502 http_503 http_504 invalid_header; # nginx自带后端检测
}
##显示后端server池中server的健康状态
location /nastatus {
check_status;
access_log off;
#allow 172.1.1.1;
#deny all;
}
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
查看负载均衡服务的状态
https://IP:port/nastatus