nginx和keepalived实现nginx高可用_高可用和负载均衡区别

(1) 2024-09-24 19:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
nginx和keepalived实现nginx高可用_高可用和负载均衡区别,希望能够帮助你!!!。

一、准备工作

先安装 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 www.yeyiboy.com yeyiboy.com;

index index.html index.htm;

location / {

proxy_pass http://backend; (外部主机来访问时,通过负载均衡器,采用轮询,平均抛给上面的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 http://blog_real_servers; }

}

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;

nginx和keepalived实现nginx高可用_高可用和负载均衡区别_https://bianchenghao6.com/blog__第1张

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 www.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 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 www.yeyiboy.com yeyiboy.com;

location / {

root html;

index index.html index.htm index.php;

proxy_pass http://www.yeyiboy.com;

}

}

server {

listen 80;

server_name blog.yeyiboy.com;

location / {

root html;

index index.html index.htm index.php;

proxy_pass http://blog.yeyiboy.com;

}

}

server {

listen 80;

server_name bbs.yeyiboy.com;

location / {

root html;

index index.html index.htm index.php;

proxy_pass http://bbs.yeyiboy.com;

}

}

}

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复