Nginx反向代理负载均衡配置
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部署
[root@web-node1 ~]# rpm -ivh \
http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
[root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
##部署web-node1 httpd服务
[root@web-node1 ~]# yum install -y httpd
[root@web-node1 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@web-node1 ~]# systemctl start httpd
[root@web-node1 ~]# echo "web-node1.com" > /var/www/html/index.html
[root@web-node1 ~]# curl http://192.168.90.201:8080/
web-node1.com
web-node2.com部署
[root@web-node1 ~]# rpm -ivh \
http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
[root@web-node1 ~]# yum install -y gcc glibc gcc-c++ make screen tree lrzsz
##部署web-node2 httpd服务
[root@web-node2 ~]# yum install -y httpd
[root@web-node2 ~]# sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
[root@web-node2 ~]# systemctl start httpd
[root@web-node2 ~]# echo "web-node2.com" > /var/www/html/index.html
[root@web-node2 ~]# curl http://192.168.90.202:8080/
web-node2.com
3.反向代理部署
Nginx 源码编译安装,使其支持4层,并监听80端口
[root@lb-node1 ~]# useradd -s /sbin/nologin -M www
[root@lb-node1 ~]# cd /usr/local/src/
[root@lb-node1 src]# wget http://nginx.org/download/nginx-1.15.8.tar.gz
[root@lb-node1 src]# tar xf nginx-1.15..tar.gz
[root@lb-node1 src]# cd nginx-1.15.
[root@lb-node1 nginx-1.10.]# ./configure --prefix=/usr/local/nginx-1.15. \
--user=www --group=www --with-http_ssl_module \
--with-http_stub_status_module --with-http_sub_module --with-file-aio --with-stream
[root@lb-node1 nginx-1.10.]# make && make install
[root@web-node1 ~]# ln -s /usr/local/nginx-1.15./ /usr/local/nginx
## 测试配置并启动Nginx
[root@lb-node1 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.15./conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.15./conf/nginx.conf test is successful
[root@lb-node1 ~]# /usr/local/nginx/sbin/nginx
3.1配置Nginx7层反向代理
1.配置Nginx反向代理
##http段配置
upstream web-cluster {
# ip_hash; #会话保持,如果有登录认证的网站需要开启
server 192.168.90.201: weight= max_fails= fail_timeout=;
server 192.168.90.202: weight= max_fails= fail_timeout=;
}
server {
listen ;
server_name 192.168.90.203;
location / {
proxy_pass http://web-cluster;
include proxy.conf;
}
}
测试代理
[root@lb-node1 ~]# curl http://192.168.90.203/
web-node1.com
[root@lb-node1 ~]# curl http://192.168.90.203/
web-node2.com
[root@lb-node1 ~]# curl http://192.168.90.203/
web-node1.com
[root@lb-node1 ~]# curl http://192.168.90.203/
web-node2.com
2.通过分组方式,以及User-agent实现不同代理
#http段配置
upstream static-cluster {
server 192.168.90.201: weight= max_fails= fail_timeout=;
}
upstream dynamic-cluster {
server 192.168.90.202: weight= max_fails= fail_timeout=;
}
upstream default-cluster {
server 192.168.90.202: weight= max_fails= fail_timeout=;
}
#需要配置本地host解析测试
server {
listen ;
server_name nginx.domain.com;
location / {
if ($http_user_agent ~* "Firefox"){
proxy_pass http://static-cluster;
}
if ($http_user_agent ~* "Chrome") {
proxy_pass http://dynamic-cluster;
}
proxy_pass http://default-cluster;
}
}
测试分组
##默认浏览器交给default处理
[root@lb-node1 ~]# curl http://nginx.domain.com
web-node2.com

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

谷歌浏览器交给dynamic-cluster处理
3.2配置Nginx4层反向代理
配置ssh以及msql反向代理
stream {
upstream ssh_proxy {
hash $remote_addr consistent;
server 192.168.90.201:;
}
upstream mysql_proxy {
hash $remote_addr consistent;
server 192.168.90.202:;
}
server {
listen ;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass ssh_proxy;
}
server {
listen ;
proxy_connect_timeout 1s;
proxy_timeout 300s;
proxy_pass mysql_proxy;
}
}
2222端口代理至于node1的SSH、3333端口代理至于node2的MYSQL
## 测试连接ssh
[root@lb-node1 ~]# ssh -p2222 root@192.168.90.203
root@192.168.90.203's password:
Last login: Wed Oct :: from 192.168.80.143
[root@web-node1 ~]#
## 测试连接mysql
[root@lb-node1 ~]# mysql -h192.168.90. -uroot -p1 -P3333
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is
Server version: 5.5.-MariaDB MariaDB Server
Copyright (c) , , Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
Nginx反向代理负载均衡配置的更多相关文章
- Centos7.4 Nginx反向代理+负载均衡配置
Ningx是一款高性能的HTTP和反向代理服务器,配置起来也比较简单. 测试环境: 172.16.65.190 Nginx-反向代理 172.16.65.191 Ningx-Web 172.16.65 ...
- Linux 下 Nginx 反向代理 负载均衡配置
转载请注明出处:http://blog.csdn.net/smartbetter/article/details/52036350 上一篇分享了 Nginx + JDK + Tomcat + MySQ ...
- Nginx 反向代理 负载均衡 虚拟主机配置
Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...
- 【转】Nginx 反向代理 负载均衡 虚拟主机配置
原文:http://www.cnblogs.com/itdragon/p/8059000.html Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代 ...
- Nginx 反向代理 负载均衡 虚拟主机
Nginx 反向代理 负载均衡 虚拟主机配置 通过本章你将学会利用Nginx配置多台虚拟主机,清楚代理服务器的作用,区分正向代理和反向代理的区别,搭建使用Nginx反向搭理和负载均衡,了解Nginx常 ...
- 十.nginx反向代理负载均衡服务实践部署
期中集群架构-第十章-nginx反向代理负载均衡章节章节====================================================================== 0 ...
- Centos 7.6配置nginx反向代理负载均衡集群
一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76 nginx负载均衡器 192.168.2.82 web ...
- Centos 7配置nginx反向代理负载均衡集群
一,实验介绍 利用三台centos7虚拟机搭建简单的nginx反向代理负载集群, 三台虚拟机地址及功能介绍 192.168.2.76 nginx负载均衡器 192.168.2.82 web ...
- 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器
Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...
随机推荐
- yii在哪些情况下可以加载yiilite.php?
yii权威指南上说,在开启apc缓存的情况下,可以加载yiilite.php提升性能.我有以下几点疑问: 1.开启apc缓存的情况下,引入yiilite.php能提升性能的原因是因为缓存了opcode ...
- C. Serval and Parenthesis Sequence 【括号匹配】 Codeforces Round #551 (Div. 2)
冲鸭,去刷题:http://codeforces.com/contest/1153/problem/C C. Serval and Parenthesis Sequence time limit pe ...
- OpenStack Grizzly详细安装指导
一.环境介绍: 控制节点 eth0 (10.10.10.51), eth1 (192.168.100.51) 网络节点 eth0 (10.10.10.52), eth1 (10.20.20.52), ...
- 解决:在pom.xml处理添加testng依赖之外,需对testng进行关联
问题描述:当maven项目中下载了testng包,在调用后,执行maven test,未执行testng.xml中指定的测试类. 解决:在pom.xml处理添加testng依赖之外,需对testng进 ...
- python js 处理弹窗图片
内置函数 : driver.execute_script() 2.自定义弹窗 由于alert弹窗不美观,现在大多数网站都会使用自定义弹窗,使用Selenium自带的方法就驾驭不了了,此时就要搬出JS大 ...
- 一个简单好用的http服务器
http-server 是一个简单的零配置命令行HTTP服务器, 基于 nodeJs. 如果你不想重复的写 nodeJs 的 web-server.js, 则可以使用这个. 安装 (全局安装加 -g) ...
- KVM虚拟机IO处理过程(一) ----Guest VM I/O 处理过程
虚拟化技术主要包含三部分内容:CPU虚拟化,内存虚拟化,设备虚拟化.本系列文章主要描述磁盘设备的虚拟化过程,包含了一个读操作的I/O请求如何从Guest Vm到其最终被处理的整个过程.本系列文章中引用 ...
- 【LeetCode2】Add Two Numbers★★
题目描述: 解题思路: 给定两个链表(代表两个非负数),数字的各位以倒序存储,将两个代表数字的链表想加获得一个新的链表(代表两数之和). 如(2->4->3)(342) + (5-> ...
- bootstrap-treeview使用
1.数据 var tree = [{ text: "车型A", nodes: [{ text: "车系1", }, { text: "车系2" ...
- GoldData学习实例-采集官网新闻数据
概述 在本节中,我们将讲述抓取政府官网地方新闻.并将抓取的新闻数据融入到以下两张数据表news_site和news中. news_site(新闻来源) 字段 类型 说明 id bigint 主键,自动 ...