linux下配置nginx负载均衡例子
准备2台虚拟机:
分别在两个虚拟机上安装tomcat,并在服务器A安装nginx,其中nginx端口设置为了 70。
服务器A的tomcat安装目录:
服务器B的tomcat安装目录:
服务器A的nginx安装目录:
准备test.jsp文件,分别上传到tomcat的 ROOT 目录下:
上传到服务器A的test.jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Page</title>
</head>
<body>
Test1 Page!!!<br/>
remote ip : <%=request.getHeader("X-real-ip") %> <br/>
nginx server ip : <%=request.getRemoteAddr()%>
</body>
</html>
上传到服务器B 的test.jsp :
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test Page</title>
</head>
<body>
Test2 Page!!!<br/>
remote ip : <%=request.getHeader("X-real-ip") %> <br/>
nginx server ip : <%=request.getRemoteAddr()%>
</body>
</html>
其中的 request.getHeader("X-real-ip") 用来获取在nginx里面设置的 客户端的真实IP,request.getRemoteAddr() 获取的是代理服务器nginx的IP。
nginx 配置文件nginx.conf :
#user nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; 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 ;
keepalive_timeout ; #gzip on;
#
#
#webapp
upstream myapp {
server 192.168.85.3: weight= max_fails= fail_timeout=30s;
server 192.168.85.4: weight= max_fails= fail_timeout=30s;
} server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
#设置客户端真实ip地址
proxy_set_header X-real-ip $remote_addr;
#负载均衡反向代理
proxy_pass http://myapp;
root html;
index index.html index.htm;
} #配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat
#location ~ \.jsp$ {
#设置客户端真实ip地址
# proxy_set_header X-real-ip $remote_addr;
# proxy_pass http://192.168.85.3:;
#} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 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;
# }
#}
#
server {
listen ;
server_name lhy.com; access_log logs/lhy.access.log main; location /{
#正则表达式匹配uri方式:在/usr/local/nginx/lhy下建立一个test1234.html 然后使用正则匹配
#location ~ test {
#重写语法:if return (条件 = ~ ~*)
#if ($remote_addr = 192.168.85.1) {
# return ;
#} if ($http_user_agent ~* firefox) {
rewrite ^.*$ /firefox.html;
break;
} root lhy;
index index.html;
} location /goods {
rewrite "goods-(\d{1,5})\.html" /goods-ctrl.html;
root lhy;
index index.html;
}
}
}
启动nginx、两台tomcat,访问 反向代理服务器nginx:http://192.168.85.3:70/test.jsp。
第一次访问,访问的是服务器A:
第二次访问,访问的是服务器B:
nginx+tomcat实现的 反向代理+负载均衡至此实现。
linux下配置nginx负载均衡例子的更多相关文章
- Mac下配置Nginx负载均衡
1.首先在Mac下安装Nginx(可参考我的另一篇随笔http://www.cnblogs.com/malcolmfeng/p/6896703.html). 2.安装Tomcat,下载后,解压,bin ...
- linux下配置nginx反向代理例子
官方说明: 例子: 虚拟机ip:192.168.85.3,物理机VMware Network Adapter VMnet8 ip:192.168.85.1 1,准备tomcat 准备一tomcat, ...
- Linux配置Nginx负载均衡
nginx配置负载均衡其实很简单,一直还以为负载均衡是个很高端人士玩的 首先先了解下负载均衡,假设一个场景,如果有1000个客户同时访问你服务器时,而你只有一台服务器的Nginx,且只有一个MySQL ...
- 配置nginx负载均衡
配置nginx负载均衡 执行命令:vi /usr/local/nginx/sbin/nginx/conf/nginx.conf 修改为: worker_processes 2; events { ...
- 负载均衡---在window与linux下配置nginx
最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...
- 【转】玩玩负载均衡---在window与linux下配置nginx
最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...
- 玩玩负载均衡---在window与linux下配置nginx
最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx, ...
- Linux下配置nginx,负载IIS的页面
最近研究了下Linux下的nginx结果贴一下: 反向代理概念: 一般访问流程:a=>b,a访问b服务器, 加n来做反向代理流程:a=>n=>b 负载均衡概率:a访问B站点,B站点有 ...
- 如何配置nginx负载均衡配置(轮询,权重,ip绑定)
集群是为了解决单节点无法服务高并发的情况,在集群中nginx是如何分配将来自客户端的请求 转发给服务器的 负载均衡可以提高网站的吞吐量(接受和响应),减轻单台服务器的压力 负载均衡提供了三种策略:轮询 ...
随机推荐
- 微信小程序——微信卡券的领取和查看
这里大致介绍下微信卡券的一些常见问题,不再介绍具体技术了,相关接口详见微信卡券. 1. 会员卡跟卡券一样么? 这个是一样的,至少在前端是一样处理的,最多也就是卡券设置展示不同.对于微信卡券领取和查看的 ...
- sendBroadcast无法接收消息可能原因
Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-de ...
- android 去掉activity的切换动画
在styles.xml文件中增加样式代码: <style name="AppTheme" parent="Theme.AppCompat.Light.NoActio ...
- Map 数据结构
JavaScript 的对象(Object),本质上是键值对的集合(Hash 结构),但是传统上只能用字符串当作键.这给它的使用带来了很大的限制. 为了解决这个问题,ES6 提供了 Map 数据结构. ...
- UVA 2451 Brackets sequence
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=9 ...
- 【转】comparable Interface
作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 , ...
- 小程序json字符串转为对象
小程序里json字符串转为对象使用JSON.parse()方法转变无效, 看报错提示有单引号“ ' ” 因为单引号而无效, 将单引号全改双引号即可. 报错如下: VM11050:1 thirdScri ...
- java时间与js时间
这是一个由java获取的系统时间与js获取的系统时间不一致导致的测试缺陷 定义方式: java Date date = new Date(); js var Date date2 = new Date ...
- 24.HashSet
在前篇博文(HashMap)中详细讲解了HashMap的实现过程,对于HashSet而言,它是基于HashMap来实现的,底层采用HashMap来保存元素.所以如果对HashMap比较熟悉,那么Has ...
- 使用 IncrediBuild 提升 VisualStudio 编译速度
我现在有一个 100M 的代码,需要快速去编译他,我寻找了很多方法,本文记录我找到的 IncrediBuild 用于提交编译速度. 如果一个项目存在很多不相互依赖的项目,那么使用 IncrediBui ...