haproxy反向代理

2024-04-13 05:32
文章标签 代理 haproxy 反向

本文主要是介绍haproxy反向代理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实验环境

主机名                IP           版本
haproxy        192.168.14.210    1.5.18    
web1           192.168.14.211    nginx/1.12.2
web2           192.168.14.212    nginx/1.12.2

一、haproxy

1、关闭防火墙和selinux

[root@haproxy ~]# systemctl stop firewalld
[root@haproxy ~]# systemctl disable  firewalld
[root@haproxy ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

2、yum安装haproxy

[root@haproxy ~]# yum install -y haproxy
[root@haproxy ~]# systemctl start haproxy
[root@haproxy ~]# systemctl enable  haproxy

3、开启haproxy日志记录

#查看proxy配置文件,提示添加-r和日志保存路径
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg
global# to have these messages end up in /var/log/haproxy.log you will# need to:## 1) configure syslog to accept network log events.  This is done#    by adding the '-r' option to the SYSLOGD_OPTIONS in#    /etc/sysconfig/syslog## 2) configure local2 events to go to the /var/log/haproxy.log#   file. A line like the following can be added to#   /etc/sysconfig/syslog##    local2.*                       /var/log/haproxy.log#log         127.0.0.1 local2#添加-r
[root@haproxy ~]# vim /etc/sysconfig/rsyslog 
# Options for rsyslogd
# Syslogd options are deprecated since rsyslog v3.
# If you want to use them, switch to compatibility mode 2 by "-c 2"
# See rsyslogd(8) for more details
SYSLOGD_OPTIONS="-r"[root@haproxy ~]# vim /etc/rsyslog.conf
#由于haproxy的日志是用udp传输的,所以要启用rsyslog的udp监听
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514#启用级别为local2的设备,并将该设备的所有级别的日志全部输出到/var/log/haproxy.log
local2.*                                                /var/log/haproxy.log#haproxy日志文件不会自动创建,需要手动添加
[root@haproxy ~]# touch /var/log/haproxy.log#重启服务
[root@haproxy ~]# systemctl restart rsyslog

4、修改配置文件(直接复制,修改后端主机IP)

[root@haproxy ~]# cat /etc/haproxy/haproxy.cfg 
#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global# to have these messages end up in /var/log/haproxy.log you will# need to:## 1) configure syslog to accept network log events.  This is done#    by adding the '-r' option to the SYSLOGD_OPTIONS in#    /etc/sysconfig/syslog## 2) configure local2 events to go to the /var/log/haproxy.log#   file. A line like the following can be added to#   /etc/sysconfig/syslog##    local2.*                       /var/log/haproxy.log#log         127.0.0.1 local2chroot      /var/lib/haproxypidfile     /var/run/haproxy.pidmaxconn     4000user        haproxygroup       haproxydaemon# turn on stats unix socketstats socket /var/lib/haproxy/stats#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaultsmode                    httplog                     globaloption                  httplogoption                  dontlognulloption http-server-closeoption forwardfor       except 127.0.0.0/8option                  redispatchretries                 3timeout http-request    10stimeout queue           1mtimeout connect         10stimeout client          1mtimeout server          1mtimeout http-keep-alive 10stimeout check           10smaxconn                 3000
listen statsmode httpbind 0.0.0.0:1080        #图形化管理页面端口stats enablestats hide-versionstats uri     /haproxyadmin?stats    #图形化管理页面stats realm   Haproxy\ Statisticsstats auth    admin:admin            #管理用户名和密码stats admin if TRUEfrontend http-inbind *:80mode httplog globaloption httpcloseoption logasapoption dontlognullcapture request  header Host len 20capture request  header Referer len 60default_backend serversfrontend healthcheckbind :1099mode httpoption httpcloseoption forwardfordefault_backend serversbackend servers    #后端主机组balance roundrobin    #负载server websrv1 192.168.14.211:80 check maxconn 2000server websrv2 192.168.14.212:80 check maxconn 2000

二、web1

#关闭防火墙和selinux
[root@web1 ~]# systemctl stop  firewalld
[root@web1 ~]# systemctl disable   firewalld
[root@web1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config#安装nginx并启动
[root@web1 ~]# yum install -y nginx
[root@web1 ~]# systemctl start nginx
[root@web1 ~]# systemctl enable nginx#修改主页面
[root@web1 ~]# echo "nginx web1" > /usr/share/nginx/html/index.html

三、web2

#关闭防火墙和selinux
[root@web2 ~]# systemctl stop  firewalld
[root@web2 ~]# systemctl disable   firewalld
[root@web2 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config#安装nginx并启动
[root@web2 ~]# yum install -y nginx
[root@web2 ~]# systemctl start nginx
[root@web2 ~]# systemctl enable nginx#修改主页面
[root@web1 ~]# echo "nginx web2" > /usr/share/nginx/html/index.html

四、客户端

1、访问haproxy

[root@client ~]# curl http://192.168.14.210
nginx web1
[root@client ~]# curl http://192.168.14.210
nginx web2
[root@client ~]# curl http://192.168.14.210
nginx web1

2、管理页面192.168.14.210:1080/haproxyadmin?stats

 

这篇关于haproxy反向代理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/899244

相关文章

高效+灵活,万博智云全球发布AWS无代理跨云容灾方案!

摘要 近日,万博智云推出了基于AWS的无代理跨云容灾解决方案,并与拉丁美洲,中东,亚洲的合作伙伴面向全球开展了联合发布。这一方案以AWS应用环境为基础,将HyperBDR平台的高效、灵活和成本效益优势与无代理功能相结合,为全球企业带来实现了更便捷、经济的数据保护。 一、全球联合发布 9月2日,万博智云CEO Michael Wong在线上平台发布AWS无代理跨云容灾解决方案的阐述视频,介绍了

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

proxy代理解决vue中跨域问题

vue.config.js module.exports = {...// webpack-dev-server 相关配置devServer: {host: '0.0.0.0',port: port,open: true,proxy: {'/api': {target: `https://vfadmin.insistence.tech/prod-api`,changeOrigin: true,p

Linux如何做ssh反向代理

SSH反向代理是一种通过SSH协议实现的安全远程访问方式,它允许客户端通过SSH连接到一台具有公网IP的代理服务器,然后这台代理服务器再将请求转发给内部网络中的目标主机。以下是实现SSH反向代理的步骤: 一、准备工作 确保服务器配置: 内网服务器(目标主机)和外网服务器(代理服务器)都安装了SSH服务,并且能够通过SSH进行互相访问。内网服务器上的服务(如Web服务、数据库服务等)需要在本地

将你的github仓库设置为web代理

将你的github仓库设置为web代理 废话不多说,直接上步骤 废话不多说,直接上步骤 创建一个仓库,上传静态web。 2. 设置仓库的 page 1)点击 “Settings” 如图设置

Nginx反向代理功能及动静分离实现

一:Nginx支持正向代理和反向代理 1.正向代理 正向代理,指的是通过代理服务器 代理浏览器/客户端去重定向请求访问到目标服务器 的一种代理服务。 正向代理服务的特点是代理服务器 代理的对象是浏览器/客户端,也就是对于目标服务器 来说浏览器/客户端是隐藏的。 正向代理是客户端指定让代理去访问哪个服务,代表客户端的利益。 2.反向代理 反向代理,指的是浏览器/客户端并不知道自己要

Nginx跨域运行案例:云台控制http请求,通过 http server 代理转发功能,实现跨域运行。(基于大华摄像头WEB无插件开发包)

文章目录 引言I 跨域运行案例开发资源测试/生产环境,Nginx代理转发,实现跨域运行本机开发运行 II nginx的location指令Nginx配置中, 获取自定义请求header头Nginx 配置中,获取URL参数 引言 背景:全景监控 需求:感知站点由于云台相关操作为 http 请求,http 请求受浏览器跨域限制,不能直接访问,因此需要进行 http 的代理,实

黑马程序员---代理

分析代理类的作用与原理及AOP的概念 代理的概念与作用  1.已经写好一个类,现在要为这个类增加一些功能,例如,异常处理、日志、计算方法的运行时间、事务管理、等等,你准备如何做? 现在我们写一个代理类: 保持了原来那个类的功能,又增加了你现在需要的功能。 主函数调用的时候,直接调用代理类就行了。 这就是代理类的功能。   2.编写一个与目标类具有相同接口的代理类,代理

GDB 反向调试

使用调试器时最常用的功能就是step, next, continue,这几个调试命令都是“往下执行”的, 但是很多时候会有这种需求:你在调试的过程中多跳过了几步而错过中间过程,这时候不得不重头调试一遍,非常麻烦。而GDB从7.0版本开始支持反向调试功能,也就是允许你倒退着运行程序,或者说撤销程序执行的步骤从而会到以前的状态。   直观地来看,加入你正在使用GDB7.0以上版本的调试器并且运行在

探寻 IP 代理地址繁多之因

在当今的网络天地里,IP 代理服务随处可见,且令人称奇的是,它们常常手握海量的 IP 地址可供挑选。那么,究竟是什么原因使得 IP 代理拥有如此众多的地址呢?现在,就让我们一同深入探究这个神秘现象背后的缘由。 从实际需求层面出发,不同的用户身处各异的使用场景,怀揣着不同的目的。企业在进行大规模数据采集时,往往期望避免被目标网站认定为单一来源进而遭到访问限制。在这种情况下,数量庞大的不同 IP