龙空技术网

Keepalived+ Nginx 高可用+负载均衡配置

科技之鹰 724

前言:

而今我们对“keepalivednginx详解”大致比较着重,各位老铁们都想要分析一些“keepalivednginx详解”的相关内容。那么小编也在网上搜集了一些对于“keepalivednginx详解””的相关资讯,希望小伙伴们能喜欢,姐妹们快快来了解一下吧!

一、准备工作

先安装 pcre pcre-devel openssl openssl-devel

二、nginx安装

./configure --user=YEYIBOY --group=YEYIBOY --prefix=/usr/local/nginx1.18 --with-http_stub_status_module --with-http_ssl_module

Make

Make install

/usr/local/nginx1.6.2/sbin/nginx -t 检查语法

三、编辑配置文件

打开 cd /usr/local/nginx/conf/

编辑sudo vim nginx.conf

在http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;下面增加

Upstream backend {

ip_hash; 使同一主机始终登录同一台机器

Server 192.168.160.154:80 max_fails=3 fail_timeout=30;

Server 192.168.160.155:80 max_fails=3 fail_timeout=30;

}

server {

listen 80;

server_name yeyiboy.com;

index index.html index.htm;

location / {

proxy_pass ; (外部主机来访问时,通过负载均衡器,采用轮询,平均抛给上面的192.168.160.154 192.168.160.155 主机)

proxy_set_header Host $host; (假设一台机器设置多个服务网站时,为了域名不混乱,应加这行和下行)

proxy_set_header X-Forwarded-For $remote_addr;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

主 vim /usr/local/nginx/extra/upstream01.conf

Upstream blog_real servers {

Server 10.0.0.17:80 weight=5;(权重)

Server 10.0.0.18:80 weight=5;

Server 10.0.0.19:80 weight=5;

可以去掉权重加 ip_hash;

url_hash 用在缓存服务器

}

Server { listen 80;

Server_name blog.etiantian.org;

Local / { proxy_pass ; }

}

Cd nginx.conf 添加 include extra/upstream01.conf

备:复制上面内容

Nginx支持的代理方式:proxy_pass fastcgi_pass memcached_pass

Upstream 模块应放在nginx.conf配置里的http{ }标签内

Location设置URI转发 语法: location [ =|~|~*|^~] uri {…….}

[=] 精确匹配

[~] 区分大小写

[^~] 只匹配字符串,不匹配正则表达式

[~*] 不区分大小写

[@] 指定一个命名的location 一般只用于内部重定向请求

http_proxy_module 从一台转发到另一台

proxy_set_header XForwarded-For $remote_addr 让web服务器记录用户IP

proxy_set_header Host $host 查找主机

proxy_next_upstream 健康检查

nginx 四层负载均衡配置 (events与http之间添加图中涂黄部分)

cd /usr/local/nginx1.12

./confgure --prefix=/usr/local/nginx --with-stream

make

make install

vim /usr/local/nginx/conf/nginx.conf

events {

worker_connections 1024; }

stream {

upstream mysite { server 192.168.1.1:8080; }

server { listen 3306; proxy_pass mysite; } }

http {

include mime.types;

default_type application/octet-stream;

Keepalived+nginx配置

Keepalived配置

sudo vim /etc/keepalived/keepalived.conf

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 51

priority 150

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.1.254/24

192.168.1.253/24

}

}

Nginx配置

Sudo vim /usr/local/nginx/conf/nginx.cong

upstream {

#ip_hash;

server 192.168.1.135:80 max_fails=3 fail_timeout=30;

server 192.168.1.138:80 max_fails=3 fail_timeout=30;

}

upstream blog.yeyiboy.com{

#ip_hash;

server 192.168.1.135:80 max_fails=3 fail_timeout=30;

server 192.168.1.138:80 max_fails=3 fail_timeout=30;

}

upstream bbs.yeyiboy.com{

#ip_hash;

server 192.168.1.135:80 max_fails=3 fail_timeout=30;

server 192.168.1.138:80 max_fails=3 fail_timeout=30;

}

server {

listen 80;

server_name yeyiboy.com;

location / {

root html;

index index.html index.htm index.php;

proxy_pass ;

}

}

server {

listen 80;

server_name blog.yeyiboy.com;

location / {

root html;

index index.html index.htm index.php;

proxy_pass ;

}

}

server {

listen 80;

server_name bbs.yeyiboy.com;

location / {

root html;

index index.html index.htm index.php;

proxy_pass ;

}

}

}

标签: #keepalivednginx详解 #nginx负载均衡算健康