准备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负载均衡例子的更多相关文章

  1. Mac下配置Nginx负载均衡

    1.首先在Mac下安装Nginx(可参考我的另一篇随笔http://www.cnblogs.com/malcolmfeng/p/6896703.html). 2.安装Tomcat,下载后,解压,bin ...

  2. linux下配置nginx反向代理例子

    官方说明: 例子: 虚拟机ip:192.168.85.3,物理机VMware Network Adapter VMnet8  ip:192.168.85.1 1,准备tomcat 准备一tomcat, ...

  3. Linux配置Nginx负载均衡

    nginx配置负载均衡其实很简单,一直还以为负载均衡是个很高端人士玩的 首先先了解下负载均衡,假设一个场景,如果有1000个客户同时访问你服务器时,而你只有一台服务器的Nginx,且只有一个MySQL ...

  4. 配置nginx负载均衡

    配置nginx负载均衡 执行命令:vi /usr/local/nginx/sbin/nginx/conf/nginx.conf 修改为: worker_processes  2; events {   ...

  5. 负载均衡---在window与linux下配置nginx

    最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...

  6. 【转】玩玩负载均衡---在window与linux下配置nginx

    最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx,sq ...

  7. 玩玩负载均衡---在window与linux下配置nginx

      最近有些时间,开始接触负载均衡方面的东西,从硬件F5再到Citrix Netscalar.不过因为硬件的配置虽然不复杂,但昂贵的价格也让一般用户望而却步(十几万到几十万),所以只能转向nginx, ...

  8. Linux下配置nginx,负载IIS的页面

    最近研究了下Linux下的nginx结果贴一下: 反向代理概念: 一般访问流程:a=>b,a访问b服务器, 加n来做反向代理流程:a=>n=>b 负载均衡概率:a访问B站点,B站点有 ...

  9. 如何配置nginx负载均衡配置(轮询,权重,ip绑定)

    集群是为了解决单节点无法服务高并发的情况,在集群中nginx是如何分配将来自客户端的请求 转发给服务器的 负载均衡可以提高网站的吞吐量(接受和响应),减轻单台服务器的压力 负载均衡提供了三种策略:轮询 ...

随机推荐

  1. 使用Pyquery+selenium抓取淘宝商品信息

    配置文件,配置好数据库名称,表名称,要搜索的产品类目,要爬取的页数 MONGO_URL = 'localhost' MONGO_DB = 'taobao' MONGO_TABLE = 'phone' ...

  2. 使用es6的then()方法封装jquery的ajax请求

    使用场景: jsp页面中使用jquery的ajax请求比较频繁,以前vue框架的项目用过axios,所以就想着用then()封装一个公共请求的方法,这样每次请求就不用那么麻烦的写一大堆请求参数了. 示 ...

  3. Tomcat的三种部署方式

    Tomcat是目前web开发中非常流行的Web 服务器,也就是tomcat在部署项目的时候,必须要把应用程序中所用到的jar包放到tomcat的lib目录下,然后再一起部署到服务器上. 那么tomca ...

  4. jQuery的介绍

    01-jQuery的介绍   1.为什么要使用jQuery 在用js写代码时,会遇到一些问题: window.onload 事件有事件覆盖的问题,因此只能写一个事件. 代码容错性差. 浏览器兼容性问题 ...

  5. Quartz.Net进阶之一:初识Job作业和触发器

        前几天写了一篇有关Quartz.Net入门的文章,大家感觉不过瘾,想让我在写一些比较深入的文章.其实这个东西,我也是刚入门,我也想继续深入了解一下,所以就努力看了一些资料,然后自己再整理和翻译 ...

  6. python 常用标准库

    标准库和第三方库第一手资料: 在线: 官方文档(https://docs.python.org/) 离线:交互式解释器(dir().help()函数),IPython(tab键提示.?.??) 一.  ...

  7. rpo攻击

    0 什么是RPO攻击? RPO(Relative Path Overwrite)相对路径覆盖,是一种新型攻击技术,最早由Gareth Heyes在其发表的文章中提出.主要是利用浏览器的一些特性和部分服 ...

  8. SQL学习(一.索引)

    数据库索引 作用: 提高查询速度 确保数据的唯一性 可以加速表和表之间的连接,实现表和表之间的参照完整性 使用分组和排序子句进行数据检索时,可以减少分组和排序的时间 全文检索字段进行搜素优化 分类: ...

  9. 简单利用jQuery,让前端开发不再依赖于后端的接口

    前端开发的过程中,我们免不了和后端进行联调,这时候就会出现以下的尴尬场景: 接口没写好,没法做接下来的功能 功能写好了,接口没写好,没法测这个功能 联调了,出了BUG,不知道锅在谁身上,只得陪后端耗时 ...

  10. mybatis中使用常量

    mybatis的mapper文件中项要使用常量的话${@类的全称路劲@常量名称}