1.反向代理概述

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个反向代理服务器。

环境准备:

主机名 IP地址 角色 系统
web-node1.com eth0:192.168.90.201 web-node1节点 CentOS7.2
web-node2.com eth0:192.168.90.202 web-node2节点 CentOS7.2
lb-node1.com eth0:192.168.90.203 Nginx反向代理 CentOS7.2
 

2.Node节点部署

在两台web-node节点中均使用Yum安装一个Apache用于做真实机,监听8080端口

web-node1.com部署

  1. [root@web-node1 ~]# rpm -ivh \
  2. http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
  3. [root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
  4. ##部署web-node1 httpd服务
  5. [root@web-node1 ~]# yum install -y httpd
  6. [root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
  7. [root@web-node1 ~]# systemctl start httpd
  8. [root@web-node1 ~]# echo "web-node1.com" > /var/www/html/index.html
  9. [root@web-node1 ~]# curl http://192.168.90.201:8080/
  10. web-node1.com

web-node2.com部署

  1. [root@web-node1 ~]# rpm -ivh \
  2. http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
  3. [root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
  4. ##部署web-node2 httpd服务
  5. [root@web-node2 ~]# yum install -y httpd
  6. [root@web-node2 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
  7. [root@web-node2 ~]# systemctl start httpd
  8. [root@web-node2 ~]# echo "web-node2.com" > /var/www/html/index.html
  9. [root@web-node2 ~]# curl http://192.168.90.202:8080/
  10. web-node2.com

3.反向代理部署

Nginx 源码编译安装,使其支持4层,并监听80端口

  1. [root@lb-node1 ~]# useradd -s /sbin/nologin -M www
  2. [root@lb-node1 ~]# cd /usr/local/src/
  3. [root@lb-node1 src]# wget http://nginx.org/download/nginx-1.15.8.tar.gz
  4. [root@lb-node1 src]# tar xf nginx-1.15..tar.gz
  5. [root@lb-node1 src]# cd nginx-1.15.
  6. [root@lb-node1 nginx-1.10.]# ./configure --prefix=/usr/local/nginx-1.15. \
  7. --user=www --group=www --with-http_ssl_module \
  8. --with-http_stub_status_module --with-http_sub_module --with-file-aio --with-stream
  9. [root@lb-node1 nginx-1.10.]# make && make install
  10. [root@web-node1 ~]# ln -s /usr/local/nginx-1.15./ /usr/local/nginx
  11. ## 测试配置并启动Nginx
  12. [root@lb-node1 ~]# /usr/local/nginx/sbin/nginx -t
  13. nginx: the configuration file /usr/local/nginx-1.15./conf/nginx.conf syntax is ok
  14. nginx: configuration file /usr/local/nginx-1.15./conf/nginx.conf test is successful
  15. [root@lb-node1 ~]# /usr/local/nginx/sbin/nginx

3.1配置Nginx7层反向代理

1.配置Nginx反向代理

  1. ##http段配置
  2. upstream web-cluster {
  3. # ip_hash; #会话保持,如果有登录认证的网站需要开启
  4. server 192.168.90.201: weight= max_fails= fail_timeout=;
  5. server 192.168.90.202: weight= max_fails= fail_timeout=;
  6. }
  7. server {
  8. listen ;
  9. server_name 192.168.90.203;
  10. location / {
  11. proxy_pass http://web-cluster;
  12. include proxy.conf;
  13. }
  14. }

测试代理

  1. [root@lb-node1 ~]# curl http://192.168.90.203/
  2. web-node1.com
  3. [root@lb-node1 ~]# curl http://192.168.90.203/
  4. web-node2.com
  5. [root@lb-node1 ~]# curl http://192.168.90.203/
  6. web-node1.com
  7. [root@lb-node1 ~]# curl http://192.168.90.203/
  8. web-node2.com

2.通过分组方式,以及User-agent实现不同代理

  1. #http段配置
  2. upstream static-cluster {
  3. server 192.168.90.201: weight= max_fails= fail_timeout=;
  4. }
  5. upstream dynamic-cluster {
  6. server 192.168.90.202: weight= max_fails= fail_timeout=;
  7. }
  8. upstream default-cluster {
  9. server 192.168.90.202: weight= max_fails= fail_timeout=;
  10. }
  11. #需要配置本地host解析测试
  12. server {
  13. listen ;
  14. server_name nginx.domain.com;
  15. location / {
  16. if ($http_user_agent ~* "Firefox"){
  17. proxy_pass http://static-cluster;
  18. }
  19. if ($http_user_agent ~* "Chrome") {
  20. proxy_pass http://dynamic-cluster;
  21. }
  22. proxy_pass http://default-cluster;
  23. }
  24. }

测试分组

  1. ##默认浏览器交给default处理
  2. [root@lb-node1 ~]# curl http://nginx.domain.com
  3. web-node2.com

火狐浏览器交给static-cluster处理

谷歌浏览器交给dynamic-cluster处理

 

3.2配置Nginx4层反向代理

配置ssh以及msql反向代理

  1. stream {
  2. upstream ssh_proxy {
  3. hash $remote_addr consistent;
  4. server 192.168.90.201:;
  5. }
  6. upstream mysql_proxy {
  7. hash $remote_addr consistent;
  8. server 192.168.90.202:;
  9. }
  10. server {
  11. listen ;
  12. proxy_connect_timeout 1s;
  13. proxy_timeout 300s;
  14. proxy_pass ssh_proxy;
  15. }
  16. server {
  17. listen ;
  18. proxy_connect_timeout 1s;
  19. proxy_timeout 300s;
  20. proxy_pass mysql_proxy;
  21. }
  22. }

2222端口代理至于node1的SSH、3333端口代理至于node2的MYSQL

  1. ## 测试连接ssh
  2. [root@lb-node1 ~]# ssh -p2222 root@192.168.90.203
  3. root@192.168.90.203's password:
  4. Last login: Wed Oct :: from 192.168.80.143
  5. [root@web-node1 ~]#
  6. ## 测试连接mysql
  7. [root@lb-node1 ~]# mysql -h192.168.90. -uroot -p1 -P3333
  8. Welcome to the MariaDB monitor. Commands end with ; or \g.
  9. Your MariaDB connection id is
  10. Server version: 5.5.-MariaDB MariaDB Server
  11. Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. MariaDB [(none)]>

Nginx反向代理负载均衡配置的更多相关文章

  1. Centos7.4 Nginx反向代理+负载均衡配置

    Ningx是一款高性能的HTTP和反向代理服务器,配置起来也比较简单. 测试环境: 172.16.65.190 Nginx-反向代理 172.16.65.191 Ningx-Web 172.16.65 ...

  2. Linux 下 Nginx 反向代理 负载均衡配置

    转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...

  3. Nginx 反向代理 负载均衡 虚拟主机配置

    Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...

  4. 【转】Nginx 反向代理 负载均衡 虚拟主机配置

    原文:http://www.cnblogs.com/itdragon/p/8059000.html Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代 ...

  5. Nginx 反向代理 负载均衡 虚拟主机

    Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...

  6. 十.nginx反向代理负载均衡服务实践部署

    期中集群架构-第十章-nginx反向代理负载均衡章节章节====================================================================== 0 ...

  7. Centos 7.6配置nginx反向代理负载均衡集群

    一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76    nginx负载均衡器 192.168.2.82    web ...

  8. Centos 7配置nginx反向代理负载均衡集群

    一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76    nginx负载均衡器 192.168.2.82    web ...

  9. 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器

    Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...

随机推荐

  1. django操作多数据库

    django操作多数据库 1.    添加数据库路由分配文件 在项目文件夹里创建‘database_router’文件.将下面的代码复制到该文件里. from django.conf import s ...

  2. centos下mysqlreport安装和使用

    首先查看你的机器是否安装了perl: #perl -v 显示版本号即表示已安装 然后: #yum install perl-DBD-mysql perl-DBI #yum install mysqlr ...

  3. 了解python wed 框架

    随着人工智能发展,python这门编程语言也渐渐被人们熟知.至于python为什么能AL的时代脱颖而出可以看一下旁边的网址了解一下https://blog.csdn.net/lixingshi/art ...

  4. LeetCode872. Leaf-Similar Trees

    自己的代码: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = ...

  5. 单链表(c语言实现)贼详细

    直接上代码吧 #include<stdio.h> #include<malloc.h> /* 单链表特点: 它是一种动态的储存结构,链表中每个节点占用的储存空间不是预先分配的, ...

  6. InnoDB数据页结构

    前言 ​ 关于数据库我们知道是通过内存对磁盘进行操作的,也知道数据会落实到磁盘上,但是数据在磁盘上的存储结构可能大家还不是很清楚. ​ MySQL服务器上负责对表中的数据的读取和写入的工作的部分是存储 ...

  7. Hadoop系列-zookeeper基础

    目前是刚刚初学完zookeeper,这篇文章主要是简单的对一些基本的概念进行梳理强化. zookeeper基础概念的理解 有时候计算机领域很多名词都是从一长串英文提取首字母缩写而来,但很不幸zooke ...

  8. SVG动画总结

    SVG可以在内部定义CSS动画样式,包括动画,如下面的格式: <svg> <defs> <style> </style> </defs>&l ...

  9. 【转】netty源码分析之LengthFieldBasedFrameDecoder

    原文:https://www.jianshu.com/p/a0a51fd79f62 拆包的原理 关于拆包原理的上一篇博文 netty源码分析之拆包器的奥秘 中已详细阐述,这里简单总结下:netty的拆 ...

  10. 「PKUSC2018」真实排名

    题面 题解 因为操作为将一些数字翻倍, 所以对于一个数\(x\), 能影响它的排名的的只有满足\(2y\geq x\)或\(2x>y\)的\(y\) 将选手的成绩排序,然后考虑当前点的方案 1. ...