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是如何分配将来自客户端的请求 转发给服务器的 负载均衡可以提高网站的吞吐量(接受和响应),减轻单台服务器的压力 负载均衡提供了三种策略:轮询 ...
随机推荐
- SVD及其在推荐系统中的作用
本文先从几何意义上对奇异值分解SVD进行简单介绍,然后分析了特征值分解与奇异值分解的区别与联系,最后用python实现将SVD应用于推荐系统. 1.SVD详解 SVD(singular value d ...
- CF 225C Barcode(DP)
传送门:点我 You've got an n × m pixel picture. Each pixel can be white or black. Your task is to change t ...
- java爬虫框架webmagic学习(一)
1. 爬虫的分类:分布式和单机 分布式主要就是apache的nutch框架,java实现,依赖hadoop运行,学习难度高,一般只用来做搜索引擎开发. java单机的框架有:webmagic和webc ...
- Python练习-迭代-2018.11.28
#遍历list L=['a','b','c','d'] l=[] a=0 for n in L: l.insert(a,n) a=a+1 print(l) #遍历dict里的key,导出为list L ...
- 373. Find K Pairs with Smallest Sums 找出求和和最小的k组数
[抄题]: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. D ...
- swift static func 和 class func
Swift中static func 相当于class final func.禁止这个方法被重写 clas func 可以被继承重写
- ES6 Number
1. 新增Number.EPSILON(误差检查函数)
- 每月最后一周的周六晚上21:00执行任务-crontab
0 21 * * 6 /bin/sh /root/time.sh #“6”代表周六 时间判断脚本如下: #!/bin/bash if [ "$(date -d "+7 days&q ...
- CSS学习总结4:派生选择器学习总结
派生选择器:通过依据元素在其位置的上下文关系来定义样式,你可以使标记更加简洁.派生选择器中一共分为三种:后代选择器.子元素选择器.相邻兄弟选择器. 1.初识派生选择器 实例:你希望列表中的 stron ...
- atom编辑器使用“apm install”无法响应的解决方案
工具:shadowsocks 利用ss建立代理服务,之后apm --help,得到apm的配置命令: apm - Atom Package Manager powered by https://ato ...