三、nginx实现反向代理负载均衡
1、反向代理
需求:
两个tomcat服务通过nginx反向代理
nginx服务器:192.168.101.3
tomcat1服务器:192.168.101.5
tomcat2服务器:192.168.101.6
如下图:

1.1. 启动tomcat
tomcat使用apache-tomcat-7.0.57版本,在192.168.101.5和192.168.101.6虚拟机上启动tomcat。
1.2. nginx反向代理配置
根据上边的需求在nginx.conf文件中配置反向代理,如下:
#配置一个代理即tomcat1服务器
upstream tomcat_server1 {
server 192.168.101.5:8080;
}
#配置一个代理即tomcat2服务器
upstream tomcat_server2 {
server 192.168.101.6:8080;
}
#配置一个虚拟主机
server {
listen 80; #监听端口
server_name aaa.test.com; #对外提供的域名,如果对内部,就用内网IP,或localhost
location / {
#域名aaa.test.com的请求全部转发到tomcat_server1即tomcat1服务上
proxy_pass http://tomcat_server1;
#欢迎页面,按照从左到右的顺序查找页面
index index.jsp index.html index.htm;
}
}
server {
listen 80;
server_name bbb.test.com;
location / {
#域名bbb.test.com的请求全部转发到tomcat_server2即tomcat2服务上
proxy_pass http://tomcat_server2;
index index.jsp index.html index.htm;
}
}
注:如果你的应用没在根目录但在根目录的子目录,不想用户每次访问输入www.aa.com/yingyong ,就需用到重写.
#重写url
location =/ {
rewrite ^ /yingyong last; #如果你的应用没在根目录但在根目录的子目录,不想用户每次访问输入www.aa.com/yingyong ,就需用到重写.
} location /yingyong { #获取重写位置
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://yingyong_proxy; #把请求转向真实的后端服务
}
如果在同一个域名下有多台服务器提供服务,此时需要nginx负载均衡。
2. 负载均衡
需求
nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至 tomcat服务器。
nginx负载均衡服务器:192.168.101.3
tomcat1服务器:192.168.101.5
tomcat2服务器:192.168.101.6
2.1. nginx实现负载均衡配置
根据上边的需求在nginx.conf文件中配置负载均衡,如下:
upstream tomcat_server_pool{
server 192.168.101.5:8080 weight=10; #weight默认为1,权重越高处理的请求越高
server 192.168.101.6:8080 weight=5;
}
server {
listen 80;
server_name aaa.test.com;
location / {
proxy_pass http://tomcat_server_pool;
index index.jsp index.html index.htm;
}
}
注:
节点说明:
在http节点里添加:
#定义负载均衡设备的 Ip及设备状态
upstream myServer {
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载的Server节点下添加
proxy_pass http://myServer;
upstream 每个设备的状态:
down 表示单前的server暂时不参与负载
weight 默认为1.weight越大,负载的权重就越大。
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout:max_fails 次失败后,暂停的时间。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
附贴一个我目前使用的配置,方便以后查看。
#user nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; upstream tomcat_server {
server XXX.XX.XX.XX:8080;
} #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name laoyeye.net; #charset koi8-r; #access_log logs/host.access.log main; location / {
rewrite (.*) http://www.laoyeye.net;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server { listen 80; server_name www.laoyeye.net; location / { #域名www.laoyeye.net的请求全部转发到tomcat_server即tomcat服务上
proxy_pass http://tomcat_server;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.jsp index.html index.htm; } } # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
三、nginx实现反向代理负载均衡的更多相关文章
- linux nginx服务 反向代理 负载均衡 nfs服务
一.nginx服务 1.首先软件停用firewall #systemctl stop firewalld stop:本次停用 disable:开机停用 enable:开机启用 #ps aux | gr ...
- nginx ----> nginx配置/反向代理/负载均衡
nginx [engine x]是一个HTTP和反向代理服务器,一个邮件代理服务器和一个通用的TCP/UDP代理服务器,最初由Igor Sysoev编写. 环境: Ubuntu16.04 安装ngin ...
- nginx做反向代理负载均衡 Java怎么获取后端服务器获取用户IP
nginx做反向负载均衡,后端服务器获取真实客户端ip 首先,在前端nginx上需要做如下配置: location / proxy_set_hearder host ...
- Nginx (二) Nginx的反向代理负载均衡以及日志切割
Nginx是一个高并发,高性能的服务器,可以进行反向代理以及网站的负载均衡.这些功能的运用都在配置文件中,也就是Nginx安装目录下的conf/nginx.conf. nginx.conf 1. 先来 ...
- Nginx(二) 反向代理&负载均衡
1.反向代理 当我们请求一个网站时,nginx会决定由哪台服务器提供服务,就是反向代理. nginx只做请求的转发,后台有多个tomcat服务器提供服务,nginx的功能就是把请求转发给后面的服务器, ...
- nginx+tomcat 反向代理 负载均衡配置
1.nginx的安装和配置见:http://www.cnblogs.com/ll409546297/p/6795362.html 2.tomcat部署项目到对应的服务器上面并启动,不详解 3.在ngi ...
- 如何使用Weave以及Docker搭建Nginx反向代理/负载均衡服务器
Hi, 今天我们将会学习如何使用 Weave 和 Docker 搭建 Nginx 的反向代理/负载均衡服务器.Weave 可以创建一个虚拟网络将 Docker 容器彼此连接在一起,支持跨主机部署及自动 ...
- 反向代理负载均衡-----nginx
一:集群 1.1:集群的概念 集群是一组相互独立的.通过高速网络互联的计算机,他们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高 ...
- 反向代理负载均衡之nginx
一.集群 1.1 什么是集群 集群是一组相互独立的.通过高速网络互联的计算机,它们构成了一个组,并以单一系统的模式加以管理.一个客户与集群相互作用时,集群像是一个独立的服务器.集群配置是用于提高可用性 ...
随机推荐
- android studio友盟分享
这个东西搞了整整两天真是把我搞郁闷着了,官方demo下载后,根据提示的错误,修改了一个小bug之后,便能直接运行,但是不管我如何集成到自己app上,分享时APP都会黑屏Crash,并且代码都与官方de ...
- 基于三台主机部署phpwind
PHPWind(简称:PW)的使命是让网站更具价值,让更多人从网络中享受便利,以提升生活品质. phpwind是一个基于PHP和MySQL的开源社区程序,是国内最受欢迎的通用型论坛程序之一.phpwi ...
- MySQL日期、时间相关内容
欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...
- nopCommerce 3.9 大波浪系列 之 可退款的支付宝插件(下)
一.回顾 支付宝插件源码下载地址:点击下载 上篇介绍了使用支付宝插件进行支付,全额退款,部分退款还有插件的多店铺配置,本文介绍下如何实现的. 二.前期准备 插件主要有3个功能: 多店铺插件配置 支付功 ...
- Vue实例对象的数据选项
前面的话 一般地,当模板内容较简单时,使用data选项配合表达式即可.涉及到复杂逻辑时,则需要用到methods.computed.watch等方法.本文将详细介绍Vue实例对象的数据选项 data ...
- vTPM环境部署(ubuntu)
注:1.系统:ubuntu16.04LTS2.ISO镜像:/home/huanghaoxiang/ubuntu-server.iso3.IMG路径:/home/TPM-Machine4.Login: ...
- Session获取不到的情况及解决办法(源码解析)
本博客是自己在学习和工作途中的积累与总结,仅供自己参考,也欢迎大家转载,转载时请注明出处,请尊重他人努力成果,谢谢. 1. 当有连个sessionFactory时,容易产生获取不到session的情况 ...
- winform 自定义控件的使用
c#的自定义控件还是很方便的,至少相对于c++而言. 1,当然是建立一个windows 窗体空间库,我这里就是用vs 2015 ,工程名MyControl 第二步.在自定义空间窗体内,拖放这样一组空间 ...
- 用github展示前端页面
今天尝试了一下使用github展示前端页面,还是比较简单的,平时做一些小的demo时,可以用这个方法展示自己的页面,在此记录下方法. 首先打开自己的github项目仓库,比如 https://gi ...
- Ajax 学习笔记
什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味 ...